文本模板不适用于某些单词

Text template not working with some words

我正在构建一个 CLI 来为自制 API 框架生成代码(现在生成控制器部分)。 为此,我正在使用模板,但当我在模板中使用 packagefunc 等词时,我看到模板没有生成任何内容(一个空文件)。

我想构建以下模板:

package controllers

{{- range .Methods }}
    {{ if eq .Name "Create" }}
            func ({{ firstChar $.ModelName }}c {{ title $.ModelName }}Controller) Get{{ title $.ModelName }}(c *gin.Context) {
                {{ $.ModelName }}, err := store.Find{{ title $.ModelName }}ById(c, c.Param("id"))

                if err != nil {
                    c.AbortWithError(http.StatusNotFound, helpers.ErrorWithCode("{{ $.ModelName }}_not_found", "The {{ $.ModelName }} does not exist", err))
                    return
                }

                c.JSON(http.StatusOK, {{ $.ModelName }})
            }
    {{ else if eq .Name "Get" }}
    {{ else if eq .Name "GetAll" }}
    {{ else if eq .Name "Update" }}
    {{ else if eq .Name "Delete" }}
    {{ end }}
{{- end }}

您知道如何使模板正常工作吗?

现在可以使用了,我只需在 template.New 之前添加 template.Must,它现在可以正常使用了。

path := filepath.Join("templates", "controller.tmpl")
    body, _ := ioutil.ReadFile(path)
    tmpl := template.Must(template.New("model").Funcs(funcMap).Parse(string(body)))

    var buf bytes.Buffer
    err := tmpl.Execute(&buf, selectedMethods)
    utils.Check(err)

    src, _ := format.Source(buf.Bytes())