Skip to content
/ gost Public

Generate golang struct definitions from MySQL table schema.

Notifications You must be signed in to change notification settings

hidechae/gost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codecov

gost

Generate golang struct definitions from mysql table schema.

Install

Run

go install github.com/hidechae/gost

Usage

$ gost -h
Generate golang struct definitions from MySQL table schema.

Usage:
  gost -u root --host 127.0.0.1 -P 3306 -d test_db -t suffix_% [flags]

Flags:
  -d, --database string   Database
      --encoding string   Encoding (default "utf8mb4")
  -h, --help              help for gost
      --host string       Host address (default "127.0.0.1")
  -p, --password string   Password
  -P, --port string       Port (default "3306")
  -t, --table string      table name
  -u, --user string       User name (default "root")

For example, following table exists.

> desc users;
+------------+------------------+------+-----+-------------------+-----------------------------+
| Field      | Type             | Null | Key | Default           | Extra                       |
+------------+------------------+------+-----+-------------------+-----------------------------+
| id         | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| email      | varchar(255)     | NO   |     | NULL              |                             |
| name       | varchar(255)     | YES  |     | NULL              |                             |
| created_at | timestamp        | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+------------------+------+-----+-------------------+-----------------------------+

gost generate struct definition from table schema.

$ gost -u root --host 127.0.0.1 -P 3306 -d test_db -t users
type User struct {
        ID uint
        Email string
        Name *string
        Gender int8
        CreatedAt time.Time
        UpdatedAt time.Time
}

Feature

  • Table and column name converted to camel case.
  • Unsigned integer types mapped uint.
  • Nullable column mapped pointer.
  • Wildcard for table name is available. (Get all tables by -t %)
  • Struct is a SINGULAR name of table name.

About

Generate golang struct definitions from MySQL table schema.

Resources

Stars

Watchers

Forks

Languages