Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "html/template"
- "net/http"
- )
- var tpl *template.Template
- var funkcijas = template.FuncMap{
- "addt": addthree,
- "timt": timesthree,
- }
- func addthree(x int) int { return x + 3 }
- func timesthree(x int) int { return x * 3 }
- func init() {
- tpl = template.Must(template.New("").Funcs(funkcijas).ParseGlob("templates/*.gohtml"))
- }
- type person struct {
- Name string
- Age int
- }
- func (p person) AgeDbl() int { return p.Age * 2 }
- func (p person) Div(x int) int { return x / 2 }
- func (p person) Add(x int) int { return x + 5 }
- func main() {
- http.HandleFunc("/", one)
- http.Handle("/favicon.ico", http.NotFoundHandler())
- http.ListenAndServe(":8080", nil)
- }
- func one(res http.ResponseWriter, req *http.Request) {
- user1 := person{Name: "Edgars", Age: 26}
- user2 := person{Name: "Martins", Age: 29}
- users := []person{user1, user2}
- tpl.ExecuteTemplate(res, "one.gohtml", users)
- }
- ========================
- <html>
- <body>
- {{range .}}
- <b>{{.Age}}-{{.Name}}<br></b>
- {{.AgeDbl}} -> {{.AgeDbl|.Div}}<br>
- {{.Age|.Div}} -> {{.Age|.Div|.Add}}<br>
- {{.Age|.Div|.Add|addt}} -> {{.Age|.Div|.Add|addt|timt}}<br>
- {{addt .Age}} -> {{addt .Age|timt}} -> {{addt .Age|timt|.Div}}<br><br>
- {{end}}
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement