Golang模板(并将func传递给模板) [英] Golang templates (and passing funcs to template)
问题描述
错误:当我尝试访问传递给我的模板的函数时出现错误: template:struct.tpl:3:函数makeGoName未定义
任何人都可以让我知道我做错了?
模板文件(struct.tpl):
{{.data.tableName}} struct {
{{range $ key,$ value:= .data.tableData}}
{{makeGoName $ value.colName}} {{$ value.colType}}`db:{{makeDBName $ value.dbColName}},json:{{$ value.dbColName}}`
{{end}}
}
调用文件:
type tplData struct {
tableName string
tableData interface {}
}
func doStuff(){
t,err:= template.ParseFiles( templates / struct.tpl)
if err!= nil {
errorQuit(err)
}
t = t.Funcs(template.FuncMap {
makeGoName:makeGoName,
makeDBName:mak eDBName,
))
data:= tplData {
tableName:tableName,
tableData:tableInfo,
}
t.Execute(os.Stdout,data)
}
func makeGoName(name string)string {
返回名称
}
func makeDBName(name string)string {
返回名称
}
这是对于生成结构样板代码的程序(如果有人想知道为什么我在模板中这么做)。 解决方案
自定义函数在解析模板之前需要注册,否则解析器将无法分辨标识符是否是有效的函数名称。模板被设计为静态可分析的,并且这是对此的一个要求。
您可以先使用 template.New()
,除 template.ParseFiles()
功能, template.Template
类型(由 New()
返回)也有一个 Template.ParseFiles()
方法,您可以调用它。
类似这样:
t,err:= template.New( ).Funcs(template.FuncMap {
makeGoName:makeGoName,
makeDBName:makeDBName,
})。ParseFiles(templates / struct.tpl)
请注意, template.ParseFiles()
函数也会调用 template.New()
,将第一个文件的名称作为模板名称。
另外 Template.Execute()
会返回 错误
,打印看看是否没有生成输出,例如:
pre $ if $ er $ = t.Execute ); err!= nil {
fmt.Println(err)
}
I'm getting an error when I try and access a function I'm passing to my template:
Error: template: struct.tpl:3: function "makeGoName" not defined
Can anyone please let me know what I'm doing wrong?
Template file (struct.tpl):
type {{.data.tableName}} struct {
{{range $key, $value := .data.tableData}}
{{makeGoName $value.colName}} {{$value.colType}} `db:"{{makeDBName $value.dbColName}},json:"{{$value.dbColName}}"`
{{end}}
}
Calling file:
type tplData struct {
tableName string
tableData interface{}
}
func doStuff() {
t, err := template.ParseFiles("templates/struct.tpl")
if err != nil {
errorQuit(err)
}
t = t.Funcs(template.FuncMap{
"makeGoName": makeGoName,
"makeDBName": makeDBName,
})
data := tplData{
tableName: tableName,
tableData: tableInfo,
}
t.Execute(os.Stdout, data)
}
func makeGoName(name string) string {
return name
}
func makeDBName(name string) string {
return name
}
This is for a program that generates struct boilerplate code (in case anyone is wondering why I'm doing that in my template).
Custom functions need to be registered before parsing the templates, else the parser would not be able to tell whether an identifier is a valid function name or not. Templates are designed to be statically analyzable, and this is a requirement to that.
You can first create a new, undefined template with template.New()
, and besides the template.ParseFiles()
function, the template.Template
type (returned by New()
) also has a Template.ParseFiles()
method, you can call that.
Something like this:
t, err := template.New("").Funcs(template.FuncMap{
"makeGoName": makeGoName,
"makeDBName": makeDBName,
}).ParseFiles("templates/struct.tpl")
Note that the template.ParseFiles()
function also calls template.New()
under the hood, passing the name of the first file as the template name.
Also Template.Execute()
returns an error
, print that to see if no output is generated, e.g.:
if err := t.Execute(os.Stdout, data); err != nil {
fmt.Println(err)
}
这篇关于Golang模板(并将func传递给模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!