Created
September 4, 2015 05:19
-
-
Save IndianGuru/ef100313c928e0d8cd9d to your computer and use it in GitHub Desktop.
new_person.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| "os" | |
| "text/template" | |
| ) | |
| type Person struct { | |
| Name string | |
| Emails []string | |
| } | |
| const tmpl = `{{$name := .Name}} | |
| {{range .Emails}} | |
| Name is {{$name}}, email is {{.}} | |
| {{end}} | |
| ` | |
| func main() { | |
| person := Person{ | |
| Name: "Satish", | |
| Emails: []string{"[email protected]", "[email protected]"}, | |
| } | |
| t := template.New("Person template") | |
| t, err := t.Parse(tmpl) | |
| if err != nil { | |
| log.Fatal("Parse: ", err) | |
| return | |
| } | |
| err = t.Execute(os.Stdout, person) | |
| if err != nil { | |
| log.Fatal("Execute: ", err) | |
| return | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment