goctl
1. 生成文件格式
goctl api go -api userapi.api -dir ./gen
1
我们在之前生成代码的时候,文件名如果是多个字母组成,那么是小写字母连在一起,比如userhandler
但是根据不同团队或者不同人的编程风格,会有多种,比如采用驼峰式或者是snake形式
那么我们可以这么做:
goctl api go -api userapi.api -dir ./gen -style go_zero
1
-style go_zero代表snake形式。
文档地址:https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md
2. 生成proto文件
goctl rpc template -o=user.proto
1
3. 生成rpc服务代码
$ goctl rpc protoc user.proto --go_out=. --go-grpc_out=. --zrpc_out=.
1
4. model
goctl model mysql ddl -src="./*.sql" -dir="./sql/model" -c
1
类型转换规则:
| mysql dataType | golang dataType | golang dataType(if null&&default null) |
|---|---|---|
| bool | int64 | sql.NullInt64 |
| boolean | int64 | sql.NullInt64 |
| tinyint | int64 | sql.NullInt64 |
| smallint | int64 | sql.NullInt64 |
| mediumint | int64 | sql.NullInt64 |
| int | int64 | sql.NullInt64 |
| integer | int64 | sql.NullInt64 |
| bigint | int64 | sql.NullInt64 |
| float | float64 | sql.NullFloat64 |
| double | float64 | sql.NullFloat64 |
| decimal | float64 | sql.NullFloat64 |
| date | time.Time | sql.NullTime |
| datetime | time.Time | sql.NullTime |
| timestamp | time.Time | sql.NullTime |
| time | string | sql.NullString |
| year | time.Time | sql.NullInt64 |
| char | string | sql.NullString |
| varchar | string | sql.NullString |
| binary | string | sql.NullString |
| varbinary | string | sql.NullString |
| tinytext | string | sql.NullString |
| text | string | sql.NullString |
| mediumtext | string | sql.NullString |
| longtext | string | sql.NullString |
| enum | string | sql.NullString |
| set | string | sql.NullString |
| json | string | sql.NullString |
我个人建议是,这块生成的代码去拿取数据model即可,其他的最好还是自己实现。
5. 生成dockerfile
goctl docker -go hello.go
1
6. 生成k8s资源清单
$ goctl kube deploy -name redis -namespace adhoc -image redis:6-alpine -o redis.yaml -port 6379
1
7. api语法
文档地址:https://go-zero.dev/cn/docs/design/grammar
我们以官方为主,所以直接看官方文档
不使用goctl的前提下,也可以使用go-zero进行快速开发。
基础上,使用goctl加快开发速度。
