Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "html/template"
- "net/http"
- )
- type grupa struct {
- Country string
- Words []string
- Number int
- }
- var tpl *template.Template
- func init() {
- tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
- }
- func main() {
- http.HandleFunc("/", index)
- http.HandleFunc("/one", one)
- http.Handle("/favicon.ico", http.NotFoundHandler())
- http.ListenAndServe(":8080", nil)
- }
- func index(res http.ResponseWriter, req *http.Request) {
- names := []string{"janis", "kristaps", "kaspars", "madara"}
- tpl.ExecuteTemplate(res, "index.gohtml", names)
- }
- func one(res http.ResponseWriter, req *http.Request) {
- lat := []string{"janis", "karlis", "edgars"}
- usa := []string{"john", "carl", "eddy"}
- latviesu := grupa{
- Country: "Latvia",
- Words: lat,
- Number: 371,
- }
- amerikanu := grupa{
- Country: "United States",
- Words: usa,
- Number: 1,
- }
- data := []grupa{latviesu, amerikanu}
- tpl.ExecuteTemplate(res, "one.gohtml", data)
- }
- ========================================
- <html>
- <body>
- {{range $index, $element := .}}
- <li>{{$index}} - {{$element}}</li>
- {{end}}
- </body>
- </html>
- <html>
- <body>
- {{range .}}
- <b>{{.Country}} - {{.Number}}</b>
- <li>{{.Words}}</li>
- <li>{{index .Words 2}}, {{index .Words 1}}, {{index .Words 0}}</li>
- {{end}}
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement