如何在 Mustache 模板中更改为帕斯卡大小写?

问题描述 投票:0回答:1

我有一个小胡子模板,例如:

{{#attributes}}
func (event {{fileNameRoot}}) Get{{Name}}() string {
    return event.{{Name}}
}
{{/attributes}}

输出:

func (event JustATest) GetuserName() string {
    return event.userName
}

我想要的是:

func (event JustATest) GetUserName() string {
    return event.UserName
}

如何实现这一目标?我正在使用 GoLang 和这个 Mustache 库:https://github.com/hoisie/mustache

Go 代码如下所示:

data := map[string]interface{}{
    "fileNameRoot": "JustATest",
    "attributes":   schema.Attributes,
}

out, err := mustache.RenderFile("templates/test.go.mustache", data)
if err != nil {
    panic(err)
}
go mustache
1个回答
0
投票

您可以使用这个有用的库

github.com/iancoleman/strcase
将字符串转换为不同的大小写。在您的情况下,您可以在将其作为参数发送之前转换
schema.Attributes

data := map[string]interface{}{
    "fileNameRoot": "JustATest",
    "attributes":   strcase.ToCamel(schema.Attributes),
}
© www.soinside.com 2019 - 2024. All rights reserved.