テンプレートに渡す関数にアクセスしようとすると、エラーが発生します。
Error: template: struct.tpl:3: function "makeGoName" not defined
誰かが私が間違っていることを教えてもらえますか?
テンプレート ファイル (struct.tpl):
type {{.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": 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
}
これは、struct ボイラープレート コードを生成するプログラム用です (テンプレートでなぜそれを行っているのか疑問に思われる場合に備えて)。