-
When creating templates you must parse the files (html templates) and then
execute
the parsed filesExample:
t, err := template.ParseFiles("./tmpl.html") if err != nil { panic(err) } u := User{ Name: "Diego", } err = t.Execute(os.Stdout, u)
-
In order to run the code above you must be at the same path, otherwise it will throw an error. To avoid this kind of problem we should embbed with the binary.
-
Exercise:
Create a User struct containing the fields Name and Surname
type User struct { Name string Surname string }
Start parsing the html template
t, err := template.ParseFiles("./tmpl.html") if err != nil { panic(err) }
Create a new user of type User
u := User{ Name: "Diego", Surname: "Parra", }
Execute the parsed template, it should throw on the Standand Output
err = t.Execute(os.Stdout, u)