几个解放双手的 Go 开发利器

Go 开发中,我们会构造各种 struct 对象,经常会有 json、数据库表、yaml、toml 等数据结构转 strcut 的需求。这时,我们可以根据字段名和数据类型来将这些数据结构,手动地填充至 Go 代码的 strcut 。但当数据字段很多时,这种方式不但耗时耗力,还容易出现一些低级错误。

针对以上情况,本文推荐几个开箱即用的开发利器,帮助 Gopher 解放双手,拯救时间。

https://regex101.com

各种语言正则表达式包括go

JSON-to-Go
JSON-to-Go 是一个将 json 数据转换为 Go 结构体的在线服务。

地址:https://mholt.github.io/json-to-go/

TOML-to-Go
TOML-to-Go 是一个将 toml 数据转换为 Go 结构体的在线服务。

地址:https://xuri.me/toml-to-go/

YAML-to-Go
TOML-to-Go 是一个将 yaml 数据转换为 Go 结构体的在线服务。

地址:https://zhwt.github.io/yaml-to-go/

curl-to-Go
curl-to-Go 是一个将 curl 请求命令和数据格式转换为 Go 相关代码的在线服务。

地址:https://mholt.github.io/curl-to-go/

sql2struct
sql2struct 是一款根据 sql 语句自动生成 Go 结构体的 chrome 插件。

地址:https://github.com/idoubi/sql2struct

它的安装非常简单,根据上面地址中给出的使用步骤即可。

当我们需要对某个数据表,例如小菜刀本地数据库中的 rent 库,执行以下命令,拿到 SQL 定义语句。

mysql> show create table rent\G;
*************************** 1. row ***************************
Table: rent
Create Table: CREATE TABLE `rent` (
`name` varchar(100) DEFAULT NULL,
`price` int(10) DEFAULT NULL,
`area` varchar(60) DEFAULT NULL,
`number` varchar(60) DEFAULT NULL,
`structure` varchar(60) DEFAULT NULL,
`pay` varchar(60) DEFAULT NULL,
`orientaion` varchar(60) DEFAULT NULL,
`floor` varchar(60) DEFAULT NULL,
`region` varchar(100) DEFAULT NULL,
`metro` varchar(60) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified
打开 sql2struct 插件,将 SQL 建表语句置入,即可得到对应的 Go 代码 struct 信息。

当然,我们还可以通过 options 选择多种字段标签,例如上例中,选择的是 gorm 和 json。

往期推荐

Ginkgo:一款 BDD 的 Go 语言框架

师弟,给力!

详解逃逸分析

Gopher一定要会的代码自动化检查

如何有效地测试Go代码

https://mp.weixin.qq.com/s/2NatnScAZLfDjL2a_2a-EA
原文链接:https://blog.csdn.net/slphahaha/article/details/120073148

原文地址:https://www.cnblogs.com/cheyunhua/p/15409698.html