Template Customization
Overview
Section titled “Overview”goctl code generation is go based on template to implement data drive. While currently goctl code generation meets some of the code successes, the template custom can enrich code generation.
Template instructions can be consulted goctl template
Sample
Section titled “Sample”Scenes
Section titled “Scenes”Implementing unified body response, following:
{ "code": 0, "msg": "OK", "data": {} // ①}① Actual Response Data
Preparation
Section titled “Preparation”我们提前在 module 为 greet 的工程下的 response 包中写一个 Response 方法,目录树类似如下:
greet├── response│ └── response.go└── xxx...Code:
package response
import ( "net/http"
"github.com/zeromicro/go-zero/rest/httpx")
type Body struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data,omitempty"`}
func Response(w http.ResponseWriter, resp interface{}, err error) { var body Body if err != nil { body.Code = -1 body.Msg = err.Error() } else { body.Msg = "OK" body.Data = resp } httpx.OkJson(w, body)}Edit handler template
Section titled “Edit handler template”$ vim ~/.goctl/${goctl_version}/api/handler.tplReplace the template with the following
package handler
import ( "net/http" "greet/response"// ① {{.ImportPackages}})
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { {{if .HasRequest}}var req types.{{.RequestType}} if err := httpx.Parse(r, &req); err != nil { httpx.Error(w, err) return }{{end}}
l := logic.New{{.LogicType}}(r.Context(), svcCtx) {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}}) {{if .HasResp}}response.Response(w, resp, err){{else}}response.Response(w, nil, err){{end}}//②
}}① Replace with your realresponsepackage name, information
② Custom Template Content
::tip 1. If you don’t have~/.goctl/${goctl版本号}/api/handler.tplfile you can initialize the template initialization commandgoctl template format ininitialize
:
:::
Compare the template before and after
Section titled “Compare the template before and after”- Modify before
func GreetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.Request if err := httpx.Parse(r, &req); err != nil { httpx.Error(w, err) return }
l := logic.NewGreetLogic(r.Context(), svcCtx) resp, err := l.Greet(&req) // 以下内容将被自定义模板替换 if err != nil { httpx.Error(w, err) } else { httpx.OkJson(w, resp) } }}- Modified
func GreetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.Request if err := httpx.Parse(r, &req); err != nil { httpx.Error(w, err) return }
l := logic.NewGreetLogic(r.Context(), svcCtx) resp, err := l.Greet(&req) response.Response(w, resp, err) }}Modify response body comparison before and after template
Section titled “Modify response body comparison before and after template”- Modify before
{ "message": "Hello go-zero!"}- Modified
{ "code": 0, "msg": "OK", "data": { "message": "Hello go-zero!" }}Template Custom Rules
Section titled “Template Custom Rules”- Modify within valid data range provided by goctl, i.e. external variables are not supported
- Adding template file is not supported
- Variable changes not supported