第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang:幫助將函數(shù)注冊到模板中

Golang:幫助將函數(shù)注冊到模板中

Go
慕斯709654 2021-10-11 13:48:40
我試圖在我的模板中添加一個函數(shù)來“人性化”(https://gowalker.org/github.com/dustin/go-humanize)輸出,但似乎我做錯了,因為我得到了這個錯誤:恐慌:模板:dashboard.tmpl:192:未定義函數(shù)“humanizeTime”func renderTemplate(w http.ResponseWriter, name string, data map[string]interface{}) error {    //functions for templates    templateFuncs := template.FuncMap{        "humanizeTime": func(t time.Time) string {            return humanize.Time(t)        },    }    tmpl := Templates[name+".tmpl"].Funcs(templateFuncs)    w.Header().Set("Content-Type", "text/html; charset=utf-8")    err := tmpl.ExecuteTemplate(w, name, data)    if err != nil {        log.Printf("Error rendering template: %s", err.Error())        http.Error(w, err.Error(), http.StatusInternalServerError)        return errors.New("Error trying to render template")    }    return nil}補充:我在 init() 上定義和加載我的模板映射:var Templates      map[string]*template.Templateif Templates == nil {    Templates = make(map[string]*template.Template)}layouts, err := filepath.Glob("templates/*.tmpl")if err != nil {    panic(err)}for _, layout := range layouts {    Templates[filepath.Base(layout)] = template.Must(template.ParseFiles(layouts...))}我只是將我的定義更改為:for _, layout := range layouts {    Templates[filepath.Base(layout)] = template.Must(template.ParseFiles(layouts...)).Funcs(templateFuncs)}但還沒有工作,我得到了同樣的錯誤:未定義。請問有人可以幫我嗎?謝謝。
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 355 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號