Advertisement
krovn

Untitled

Nov 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.78 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.ParseFiles("templates/two.gohtml"))
  17. }
  18.  
  19. func (p person) AgeDbl() int {
  20.     return p.Age * 2
  21. }
  22.  
  23. func (p person) Dbl(x int) int {
  24.     return x * 2
  25. }
  26.  
  27. func main() {
  28.     http.HandleFunc("/", index)
  29.     http.ListenAndServe(":8080", nil)
  30. }
  31.  
  32. func index(res http.ResponseWriter, req *http.Request) {
  33.     p := person{
  34.         Name: "Janis",
  35.         Age:  24,
  36.     }
  37.     tpl.ExecuteTemplate(res, "two.gohtml", p)
  38. }
  39. /*
  40.  
  41. <body>
  42.     This might be two.gohtml? But I am not sure.
  43.     <h3>
  44.         {{.Age}}|{{.Name}}|{{.AgeDbl}}|{{.AgeDbl|.Dbl}}
  45.     </h3>
  46. </body>
  47.  
  48. This might be two.gohtml? But I am not sure.
  49. 24|Janis|48|96
  50.  
  51. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement