Advertisement
krovn

Untitled

Nov 15th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.73 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "html/template"
  5.     "net/http"
  6. )
  7.  
  8. type person struct {
  9.     Name string
  10.     Age  int
  11. }
  12.  
  13. var tpl *template.Template
  14.  
  15. func init() {
  16.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  17. }
  18.  
  19. func main() {
  20.     http.HandleFunc("/", index)
  21.     http.ListenAndServe(":8080", nil)
  22. }
  23.  
  24. func index(res http.ResponseWriter, req *http.Request) {
  25.     martins := person{
  26.         Name: "Martins",
  27.         Age:  23,
  28.     }
  29.     edgars := person{
  30.         Name: "Edgars",
  31.         Age:  27,
  32.     }
  33.     janis := person{
  34.         Name: "Janis",
  35.         Age:  24,
  36.     }
  37.     data := []person{martins, edgars, janis}
  38.     tpl.ExecuteTemplate(res, "two.gohtml", data)
  39. }
  40.  
  41. <body>
  42.     Home of two.gohtml<br />
  43.     {{range .}}
  44.         <li>{{.Name}} - {{.Age}}</li>
  45.     {{end}}
  46. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement