1 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
始終檢查 返回的錯(cuò)誤。Execute
在模板內(nèi)部,您不能引用在傳遞給模板的結(jié)構(gòu)中不存在的字段,即當(dāng)您傳遞操作時(shí)會(huì)破壞模板,當(dāng)您傳遞中斷模板時(shí)。failure{{.Signupsuccess}}success{{.Loginfailure}}
您可以使用地圖,允許引用地圖中不存在的地圖鍵
success := map[string]string{"Signupsuccess": "Your account is successfully created"}
if err := oginTmpl.Execute(w, success); err != nil {
panic(err)
}
failure := map[string]string{"Loginfailure": "Enter the correct email or password"}
if err := loginTmpl.Execute(w, failure); err != nil {
panic(err)
}
或者使用具有兩個(gè)字段的單個(gè)結(jié)構(gòu)
type TemplateData struct {
Signupsuccess string
Loginfailure string
}
success := TemplateData{Signupsuccess: "Your account is successfully created"}
if err := oginTmpl.Execute(w, success); err != nil {
panic(err)
}
failure := TemplateData{Loginfailure: "Enter the correct email or password"}
if err := loginTmpl.Execute(w, failure); err != nil {
panic(err)
}
- 1 回答
- 0 關(guān)注
- 98 瀏覽
添加回答
舉報(bào)