Go template function

后端 未结 3 2039
你的背包
你的背包 2021-02-05 02:26

It noticed a weird thing with Go templates when I try to use Funcs and FuncMap. The following code works as expected:

buffer := bytes.N         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 03:01

    ParseFiles could probably use better documentation. A template object can have multiple templates in it and each one has a name. If you look at the implementation of ParseFiles, you see that it uses the filename as the template name inside of the template object. So, name your file the same as the template object, (probably not generally practical) or else use ExecuteTemplate instead of just Execute.

提交回复
热议问题