试试这个,我已经尽力了...
package main
import "html/template"
import "os"
type data struct {
Url string
Title string
}
type show struct {
Pages []data
}
const html = `<html>
{{range .Pages}}
<div class="page"><a href="/posts/{{.Url}}">{{.Title}}</a>
</div>
{{end}}
</html>`
func show_template() {
webpage, _ := template.New("template").Parse(html)
mydata := []data{{
Url: "page-1.html",
Title: "go to page 1",
}, {
Url: "page-2.html",
Title: "go to page 2",
}, {
Url: "page-3.html",
Title: "go to page 3",
}, {
Url: "page-3.html",
Title: "go to page 3",
}}
web_data := show{mydata}
webpage.Execute(os.Stdout, web_data)
}
func main() {
show_template()
}
这就是结果..
<html>
<div class="page"><a href="/posts/page-1.html">go to page 1</a></div>
<div class="page"><a href="/posts/page-2.html">go to page 2</a></div>
<div class="page"><a href="/posts/page-3.html">go to page 3</a></div>
<div class="page"><a href="/posts/page-3.html">go to page 3</a></div>
</html>