Golang template 常見問題
————不解決隨意可以百度的問題
template 語法
- {{with .Var}} 在下文可以用{{.}}
- 判斷相等用 {{eq .x .y}}
- 獲取數組長度 {{len .papers}}
- 判斷數組長度爲0 {{eq (len .papers) 0}}
template 自定義函數
- FuncMaps
- 如果是ParseFiles,則template對象無法使用自定義函數,用如下方法解決
t, _ := template.New("user.html").Funcs(template.FuncMap{"showTime": func(ts int32) string {
return time.Unix(int64(ts), 0).Format("2006-01-02 15:04:05")
}}).ParseFiles("static/views/user.html", "static/views/ref.html")
- 調試——打印error