1. I want figure out what happens underline of package text/template.
/** * PrintPerson */ package main import ( "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{"[email protected]", "[email protected]"}, 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
时间: 2024-10-24 14:31:31