这个 golang struct 是用的什么生成的

2022-09-14 08:18:23 +08:00
 guanhui07

gorm

ddl 语句如下:

CREATE TABLE `pf_station_info` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '站点名称',
  `location` json NOT NULL COMMENT '站点所在经纬度',
  `ip_address` varchar(15) NOT NULL DEFAULT '' COMMENT '站点 ip 地址,非此 IP 数据不接收',
  `tiger_shaped` char(32) NOT NULL DEFAULT '' COMMENT '握手符号',
  `heartbeat` int unsigned NOT NULL DEFAULT '0' COMMENT '上次心跳时间戳',
  `status` int NOT NULL DEFAULT '0' COMMENT '站点状态:0 正常,非 0 为停用时间戳',
  `add_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

要生成 如下 gorm struct , not null 以及 comment 也在 tag 里 且 location 类型 是 datatypes.JSON 类型

type Station struct {
	Id          int64          `json:"id" gorm:"primaryKey;autoIncrement;comment:主键 id"`
	Name        string         `gorm:"type:varchar(20);column:name;not null;comment:站点名称" json:"name"`
	Location    datatypes.JSON `gorm:"type:json;column:location;not null;comment:站点所在经纬度" json:"location"`
	IpAddress   string         `gorm:"type:varchar(15);column:ip_address;not null;comment:站点 ip 地址,非此 IP 数据不接收" json:"ip_address"`
	TigerShaped string         `gorm:"type:char(32);column:tiger_shaped;not null;comment:握手符号" json:"tiger_shaped"`
	Heartbeat   int64          `gorm:"type:int(10);column:heartbeat;not null;comment:上次心跳时间戳" json:"heartbeat"`
	Status      int64          `gorm:"type:int(11);column:status;comment:站点状态:0 正常,非 0 为停用时间戳" json:"status"`
	AddTime     time.Time      `gorm:"type:datetime;comment:创建时间" json:"add_time,omitempty"`
	UpdateTime  time.Time      `gorm:"type:datetime;comment:修改时间" json:"update_time,omitempty"`
}

有人知道 上面的 struct 是用 什么工具生成的吗?我猜不可能手写吧。

谷歌搜了下 看了好几个 根据 ddl 生成都不一样 ,比如下面的

https://www.qetool.com/sql_json_go/sql.html

http://sql2struct.atotoa.com/

utools 里的 sql2struct 生成也不一样

有没大佬推荐个好用点的 或 跟这个 生成一模一样的。

3537 次点击
所在节点    Go 编程语言
17 条回复
treblex
2022-09-14 08:27:18 +08:00
orm 不是從 struct 生成 sql 嗎,應該是手寫的吧
guanhui07
2022-09-14 08:33:18 +08:00
@treblex #1 不能吧 先写 struct 再 根据 struct 生成 ddl 语句 吧。
那我这个 struct 怎么 生成 ddl 语句 ,用什么工具 转。

我现在想到是 ddl2struct 网上找的 不太一样 只能改改 ,因为好奇所以找了找 ,没找到 一样的。
说不定别人自己写的转换工具 ,哈哈
ikaros
2022-09-14 08:49:34 +08:00
@guanhui07 gorm v2 的 migrator interface 里面有个 create table 函数, v1 我记得就是在*gorm.DB 里面, 可以用这个函数创建表
idoubi
2022-09-14 08:56:04 +08:00
可能用我这个工具生成完,然后自己改了一些 tag ? https://dou.tools/sql2struct/?
to2false
2022-09-14 08:59:02 +08:00
推荐官方下的一个工具 https://github.com/go-gorm/gen
Johngodme
2022-09-14 09:04:03 +08:00
aeli
2022-09-14 09:06:46 +08:00
copilot 可以根据 sql 语句注释之类的自动生成
Trim21
2022-09-14 09:12:16 +08:00
应该是 gorm/gen ,具体字段生成出来的类型是可以自己设置的。
xioxu
2022-09-14 09:28:31 +08:00
历史悠久的工具 codeSmith 就可以根据 schema 生成任何代码
king888
2022-09-14 09:56:29 +08:00
不用 gorm

一般用 fraenky8/tables-to-go 生成 struct ,配合不到 500 行自己封装的 CURD 直接撸,至于 Database Design ,AutoMigrate 是直接用的 DbSchema 这款软件,爽的一批
FrankFang128
2022-09-14 10:02:06 +08:00
@king888 看起来不错
waltcow
2022-09-14 10:03:02 +08:00
结合 sql ,copilot 一直 tab 到底
keepeye
2022-09-14 10:25:43 +08:00
我都是手写 tag ,用 gorm 的 migrate 创建表
ElmerZhang
2022-09-14 11:06:58 +08:00
copilot 就可以,把 DDL 以注释的形式粘在文件开头,然后开始写 struct ,写个一两行之后,后面的就都能用 copilot 补全一路 tab 出来。搞完了再把 DDL 注释删了就行了。
dalang
2022-09-14 13:59:01 +08:00
lrvy
2022-09-14 14:16:13 +08:00
gorm 有个 gen 库 https://github.com/go-gorm/gen
timethinker
2022-09-15 12:04:45 +08:00
挺有意思,刚才用 JS 手写了一个 DDL 的解析器,可以将这种 CREATE TABLE 转换成 JSON ,然后有了 JSON 以后就可以用来生成代码了,仓促之下只做了 CREATE TABLE 这种 SQL 语句,写的也比较凌乱,可能还有一些 BUG ,不过可以自行拓展。

地址: https://jsfiddle.net/AlexMaho/b6c1utoe/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/879865

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX