例如,在go中我有x := map[string]bool{"1":true, "2":true}
我如何使用內置的模板包,將其列印到go.tmpl中?
// go.tmpl
{{x}}
渲染后,我希望它像
一樣x := map[string]bool{"1"/span>: true,"2":true}。
我有一些辦法嗎?
uj5u.com熱心網友回復:
s := `{{x}}`
x := map[string]bool{"1"/span>。true, "2": true}。
t, err := template.New("t"/span>).Funcs(template.FuncMap{
"x": func() string { return fmt。 Sprintf("x := %#v", x) },
}).Parse(s)
if err != nil {
panic(err)
}
if err := t.Execute(os.Stdout, nil); err != nil {
panic(err)
https://play.golang.org/p/Pww7-PFIWXJ
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316117.html
標籤:
上一篇:django檔案上傳問題
