preface

Gorm package is a very popular ORM package in Golang. Before using GORM for database operation, it is generally necessary to map data table fields with a Golang structure. Thus, we often need to manually write structs in go code based on the field names and types in the data table. Sometimes, this method can be very expensive when the data table has many fields. So I thought of writing a Chrome plug-in to automatically generate the Golang structure according to the DDL statement of the data table. You can configure a mapping relationship between the go structure field type and the mysql data table field type.

SQL2Struct

SQL2Struct is a Golang developer friendly Chrome plugin that automatically generates structs in Golang based on SQL statements that create tables in mysql. It is good to associate mysql tables with ORM structures.

Github address: github.com/mikemintang…

Directions for use

  1. Download the Chrome extension file: Click Download

  2. Install the extension

  1. Get the SQL statement that generates the data table in mysql

show create table users\G;

  1. Go to the main page of the plug-in and paste the SQL statement obtained in the previous step into the input box on the left

  2. Copy the struct generated on the right and paste it into the Golang code

Configuration instructions

Currently, there are only three configuration items

  • Gorm: When this configuration item is turned on, structs are generated with each field containing similargorm:column:"id"That kind of information.
  • Json: If this configuration item is enabled, each field will contain similar structs when they are generatedjson:"id"That kind of information.
  • TypeMap: Defines the mapping between the mysql table field types and the GO field types. The mapping will be installed to generate structures during data parsing.

Todolist

  • [] Supports more mapping between mysql types and GO types
  • [] Supports custom field configuration for conversion
  • [] Regular expression optimization
  • [] Convert the plural form of data table name to the singular form of struct name
  • [] Added primary key and index conversion support

Contribution

Fork code, raise issues, or pull requests are welcome

SQL2Struct is a Chrome plugin that automatically generates golang structures based on SQL statements