Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "html/template"
- "log"
- "net/http"
- )
- var tpl *template.Template
- var funkcijas = template.FuncMap{
- "dbl": double,
- "add": add,
- }
- type person struct {
- Name string
- Age int
- }
- func double(x int) int {
- return x * 2
- }
- func add(x int) int {
- return x + 2
- }
- func init() {
- tpl = template.Must(template.New("").Funcs(funkcijas).ParseFiles("templates/two.gohtml"))
- }
- func main() {
- http.HandleFunc("/", index)
- http.ListenAndServe(":8080", nil)
- }
- func index(res http.ResponseWriter, req *http.Request) {
- user1 := person{
- Name: "janis",
- Age: 24,
- }
- user2 := person{
- Name: "Edgars",
- Age: 27,
- }
- user := []person{user1, user2}
- err := tpl.ExecuteTemplate(res, "two.gohtml", user)
- if err != nil {
- log.Fatalln(err)
- }
- }
- <body>
- Home of two.gohtml<br /><br />
- {{range .}}
- {{.Name}} - {{.Age}}<br />
- {{dbl .Age}}
- {{add .Age}}<br /><br />
- {{end}}
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement