亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

模板和自定義功能;恐慌:功能未定義

模板和自定義功能;恐慌:功能未定義

Go
慕無忌1623718 2021-06-08 13:19:46
使用html/template我試圖在模板中使用我自己的功能之一。不幸的是,我無法使用 go 模板的函數映射功能。我得到的只是以下錯誤:% go run test.gopanic: template: tmpl.html:5: function "humanSize" not defined[...]簡化的測試用例如下 ( test.go):package mainimport (    "html/template"    "io/ioutil"    "net/http"    "strconv")var funcMap = template.FuncMap{    "humanSize": humanSize,}var tmplGet = template.Must(template.ParseFiles("tmpl.html")).Funcs(funcMap)func humanSize(s int64) string {    return strconv.FormatInt(s/int64(1000), 10) + " KB"}func getPageHandler(w http.ResponseWriter, r *http.Request) {    files, _ := ioutil.ReadDir(".")    if err := tmplGet.Execute(w, files); err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)    }}func main() {    http.HandleFunc("/", getPageHandler)    http.ListenAndServe(":8080", nil)}我有以下簡單模板 ( tmpl.html):<html><body>    {{range .}}    <div>        <span>{{.Name}}</span>        <span>{{humanSize .Size}}</span>    </div>    {{end}}</body></html>這是 1.1.1。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號