Developer Tools

SQL to Go Struct

Turn CREATE TABLE statements from MySQL, PostgreSQL or SQLite into Go structs with json, db and GORM tags.

Struct tags
Nullable columns

2 tables, 14 columns

Go
package model

import (
	"encoding/json"
	"time"
)

// User Registered users
type User struct {
	ID        uint64          `json:"id" db:"id"`
	Email     string          `json:"email" db:"email"` // login email
	Nickname  *string         `json:"nickname" db:"nickname"`
	IsActive  bool            `json:"is_active" db:"is_active"`
	Balance   float64         `json:"balance" db:"balance"`
	Settings  json.RawMessage `json:"settings" db:"settings"`
	CreatedAt time.Time       `json:"created_at" db:"created_at"`
	DeletedAt *time.Time      `json:"deleted_at" db:"deleted_at"`
}

type OrderItem struct {
	ID        int64      `json:"id" db:"id"`
	OrderID   string     `json:"order_id" db:"order_id"`
	Sku       string     `json:"sku" db:"sku"`
	Quantity  int32      `json:"quantity" db:"quantity"`
	UnitPrice float64    `json:"unit_price" db:"unit_price"`
	ShippedAt *time.Time `json:"shipped_at" db:"shipped_at"`
}

Columns without NOT NULL are treated as nullable. PostgreSQL array columns map to Go slices, which need a driver type such as pq.StringArray to scan with database/sql.

About this tool

When a Go service sits on an existing database, its model structs should mirror the tables exactly. Copy the CREATE TABLE statements from a migration, a schema dump or SHOW CREATE TABLE, paste them here, and get one Go struct per table with json, db (sqlx) and GORM tags, formatted the way gofmt would.

The converter understands MySQL, PostgreSQL and SQLite syntax: quoted names, schema-qualified tables, UNSIGNED, AUTO_INCREMENT, SERIAL and IDENTITY columns, table-level PRIMARY KEY, column and table comments, and PostgreSQL arrays. Nullable columns become pointers or sql.Null types, as you prefer. Your schema stays in your browser.

How to use SQL to Go Struct

  1. Paste one or more CREATE TABLE statements, or open a .sql file. Other statements such as INSERT or CREATE INDEX are ignored.
  2. Choose the tags: json for APIs, db for sqlx, gorm for GORM models.
  3. Choose how nullable columns are typed: *T, sql.NullT or plain T.
  4. Optionally map DECIMAL to string and add a GORM TableName() method, then copy or download models.go.

Examples

A MySQL table

Input
CREATE TABLE products (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, price DECIMAL(10,2), stock INT NOT NULL DEFAULT 0);
Result
type Product struct { ID int32 `json:"id" db:"id"`; Name string; Price *float64; Stock int32 }

The table name products becomes the singular struct name Product.

GORM tags with sql.Null types

Input
Same table, tags: gorm, nullable columns: sql.NullT
Result
Price sql.NullFloat64 `gorm:"column:price;type:decimal(10,2)"`; ID int32 `gorm:"column:id;primaryKey;autoIncrement;type:int"`

A PostgreSQL table

Input
CREATE TABLE events (id BIGSERIAL PRIMARY KEY, tags TEXT[], payload JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT now());
Result
ID int64; Tags []string; Payload json.RawMessage; CreatedAt time.Time, with imports for encoding/json and time

How SQL types map to Go

  • TINYINT(1), BOOLEAN and BIT(1) → bool. TINYINT → int8, SMALLINT → int16, INT → int32, BIGINT → int64; UNSIGNED adds the u prefix (uint64).
  • SERIAL → int32 and BIGSERIAL → int64, marked as auto-increment.
  • FLOAT and REAL → float32, DOUBLE → float64. DECIMAL and NUMERIC → float64, or string when you need exact values such as money.
  • CHAR, VARCHAR, TEXT, ENUM, UUID and TIME → string. DATE, DATETIME and TIMESTAMP → time.Time.
  • JSON and JSONB → json.RawMessage; BLOB, BINARY and BYTEA → []byte; PostgreSQL arrays → slices such as []string.

Pointers or sql.Null types?

A column without NOT NULL can hold NULL, which a plain Go string or int cannot represent: scanning NULL into it fails. Pointers (*string) work with database/sql, sqlx and GORM, encode to null in JSON and read naturally. sql.NullString and friends avoid allocations but encode to objects like {"String":"","Valid":false} in JSON. Plain types are fine only if you use COALESCE in queries or are sure the column never holds NULL.

Frequently asked questions

Everything you need to know about this tool and data security.

Is my schema uploaded?

No. Parsing and generation happen in your browser, so internal table and column names stay on your device.

Which databases are supported?

CREATE TABLE syntax from MySQL and MariaDB, PostgreSQL and SQLite, including backtick, double-quote and bracket quoting. Other dialects usually work if they use standard column definitions.

Why is DECIMAL a float64 by default?

It is the simplest type to work with, but floats cannot represent all decimal values exactly. For money, enable “DECIMAL as string” or use a decimal library such as shopspring/decimal.

How do I read PostgreSQL array columns?

database/sql cannot scan arrays into plain slices. Use pq.StringArray (lib/pq) or pgtype types (pgx), or let GORM handle them with a serializer.

What is the TableName() method for?

GORM derives table names by pluralising the struct name. TableName() fixes the name explicitly, which matters when your tables do not follow that convention.

Is my data safe when using this tool?

Yes, 100% safe. This tool operates entirely inside your web browser using client-side JavaScript and WebAssembly. Your data never touches any external server.