Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "html/template"
- "net/http"
- )
- type person struct {
- Name string
- Age int
- }
- var tpl *template.Template
- func init() {
- tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
- }
- func main() {
- http.HandleFunc("/", index)
- http.ListenAndServe(":8080", nil)
- }
- func index(res http.ResponseWriter, req *http.Request) {
- martins := person{
- Name: "Martins",
- Age: 23,
- }
- edgars := person{
- Name: "Edgars",
- Age: 27,
- }
- janis := person{
- Name: "Janis",
- Age: 24,
- }
- data := []person{martins, edgars, janis}
- tpl.ExecuteTemplate(res, "two.gohtml", data)
- }
- <body>
- Home of two.gohtml<br />
- {{range .}}
- <li>{{.Name}} - {{.Age}}</li>
- {{end}}
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement