模板(Template)是数据驱动生成的基础,所有的代码(rest api、rpc、model、docker、kube)生成都会依赖模板, 默认情况下,模板生成器会选择内存中的模板进行生成,而对于有模板修改需求的开发者来讲,则需要将模板进行落盘, 从而进行模板修改,在下次代码生成时会加载指定路径下的模板进行生成。
首先我们需要了解 go 的 template 库 https://pkg.go.dev/text/templatepkg.go.dev/text/template Template 库主要用于根据模板生成文件, 常用于 html 模板文件生成,goctl 也是通过 template 库设定自己的模板进行生成。 常用函数 New 新建模板 ParseFS 从文件系统读取模板 ParseFiles 读取模板文件 Go Zero 内置...
template=../../goctl/1.6.1 echo "开始创建库:$dbname 的表:$2" goctl model mysql datasource -url="${username}:${passwd}@tcp(${host}:${port})/${dbname}" -table="${tables}" -dir="${modeldir}" -cache=true --home="${template}" --style=goZero 模板定制化使用方法 相关命令使用...
echo "开始创建库:$dbname 的表:$2" goctl model mysql datasource -url="${username}:${passwd}@tcp(${host}:${port})/${dbname}" -table="${tables}" -dir="${modeldir}" -cache=true --home="${template}" --style=goZero 使用GitBash打开deploy/script/mysql目录,执行脚本 将相关代码放到mo...
【注】有人会说,每次使用goctl都要过来手动改,那不是要麻烦死了,这里我们使用go-zero给我们提供的template模版功能(还不知道这个的就要去官方文档学习一下了),修改一下handler生成模版即可,整个项目的模版文件放在deploy/goctl下,这里hanlder修改的模版在deploy/goctl/1.2.3-cli/api/handler.tpl ...
template=../../goctl/1.6.1 echo "开始创建库:$dbname 的表:$2" goctl model mysql datasource -url="${username}:${passwd}@tcp(${host}:${port})/${dbname}" -table="${tables}" -dir="${modeldir}" -cache=true --home="${template}" --style=goZero ...
passwd=xxxxxecho"开始创建库:$dbname的表:$2"goctl model mysql datasource -url="${username}:${passwd}@tcp(${host}:${port})/${dbname}"-table="${tables}"-dir="${modeldir}"-cache=true--home="${template}"--style=goZero 这样,我们就可以很方便的使用./genModel.sh生成model,而不是需要拼...
goctl model mysql datasource -url="${username}:${passwd}@tcp(${host}:${port})/${dbname}" -table="${tables}" -dir="${modeldir}" -cache=true --home="${template}" --style=goZero #!/usr/bin/env bash # 使用方法: # ./genModel.sh 数据库名 表名称 # 比如: # ./genModel.sh ...
Goctl自定义模版template: 将项目目录下的data/goctl文件夹内容copy到家目录的.goctl中,goctl在生成代码时候会优先根据这个模版下内容生成 $ cp -r data/goctl ~/.goctl 1. 2、安装protoc 链接:https://github.com/protocolbuffers/protobuf/releases ...
当当当当~~~goctl的template来咯https://www.yuque.com/tal-tech/go-zero/mkpuit 然后修改~/.goctl/api/handler.tpl: packagehandlerimport("net/http"{{.ImportPackages}} )func{{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {returnfunc(w http.ResponseWriter, r *http.Request){ ...