首页 > 代码库 > package text/html
package text/html
1. I want figure out what happens underline of package text/template.
/** * PrintPerson */package mainimport ( "fmt" "html/template" "os")type Person struct { Name string Age int Emails []string Jobs []*Job}type Job struct { Employer string Role string}const templ = `The name is {{.Name}}.The age is {{.Age}}.{{range .Emails}} An email is {{.}}{{end}}{{with .Jobs}} {{range .}} An employer is {{.Employer}} and the role is {{.Role}} {{end}}{{end}}`func main() { job1 := Job{Employer: "Monash", Role: "Honorary"} job2 := Job{Employer: "Box Hill", Role: "Head of HE"} person := Person{ Name: "jan", Age: 50, Emails: []string{"jan@newmarch.name", "jan.newmarch@gmail.com"}, Jobs: []*Job{&job1, &job2}, } t := template.New("Person template") t, err := t.Parse(templ) checkError(err) err = t.Execute(os.Stdout, person) checkError(err)}func checkError(err error) { if err != nil { fmt.Println("Fatal error ", err.Error()) os.Exit(1) }}
the sample code from http://jan.newmarch.name/go/template/chapter-template.html
2. analysis
package text/html
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。