2014-12-22 43 views
-5

對不起,我初學者,我讀了golang.docs,但沒有很好理解。 i`ve:index.html的:在golang上使用html模板

<html> 
<head> 
</head> 
<body> 
<form action type="checkbox" name="test" value="A" {{.checked}}> 
<input type="submit" value="save"> 
</body> 
</html> 

在main.go 如果用戶點擊保存按鈕,然後選中複選框重定向頁面,並顯示選中複選框

+2

問題是....? – topskip

+0

如何將值設置爲{{.checked}} – Student

+1

您應該編輯您的帖子,並寫下您目前爲止所做的以及您遇到問題的位置。否則,我們無法幫助您。 – topskip

回答

0

您可以在地圖中發送變量。例如:

package main 

import (
    "bytes" 
    "fmt" 
    "text/template" 
) 

func main() { 
    t, _ := template.New("hi").Parse("Hi {{.name}}") 
    var doc bytes.Buffer 
    t.Execute(&doc, map[string]string{"name": "Peter"}) 
    fmt.Println(doc.String()) //Hi Peter 
} 
0

.在Go代碼中定義。

請提供執行模板的你去的代碼片段,類似如下代碼:

t, _ := template.ParseFiles(tmpl + ".html") 
    t.Execute(w, data) // the data must feature the field "checked" 

或者

templates.ExecuteTemplate(w, tmpl+".html", data) // the data must feature the field "checked" 

您可以通過任何類型(接口{})來一個執行模板爲「數據」的函數。通常它是一個Struct或一個Map [string]字符串。

如何設置檢查 可能「檢查」是根據發佈的表單在main.go處理程序中設置的。

閱讀文檔並更好地解釋它。請