Golang template 高級問題

Golang template 常見問題

————不解決隨意可以百度的問題

  1. template 語法

    • {{with .Var}} 在下文可以用{{.}}
    • 判斷相等用 {{eq .x .y}}
    • 獲取數組長度 {{len .papers}}
    • 判斷數組長度爲0 {{eq (len .papers) 0}}
  2. 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

Golang 項目地址

https://github.com/gajanlee/Posger

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章