Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.31 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "html/template"
  5.     "net/http"
  6. )
  7.  
  8. type grupa struct {
  9.     Country string
  10.     Words   []string
  11.     Number  int
  12. }
  13.  
  14. var tpl *template.Template
  15.  
  16. func init() {
  17.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  18. }
  19.  
  20. func main() {
  21.     http.HandleFunc("/", index)
  22.     http.HandleFunc("/one", one)
  23.     http.Handle("/favicon.ico", http.NotFoundHandler())
  24.     http.ListenAndServe(":8080", nil)
  25. }
  26.  
  27. func index(res http.ResponseWriter, req *http.Request) {
  28.     names := []string{"janis", "kristaps", "kaspars", "madara"}
  29.     tpl.ExecuteTemplate(res, "index.gohtml", names)
  30. }
  31.  
  32. func one(res http.ResponseWriter, req *http.Request) {
  33.     lat := []string{"janis", "karlis", "edgars"}
  34.     usa := []string{"john", "carl", "eddy"}
  35.     latviesu := grupa{
  36.         Country: "Latvia",
  37.         Words:   lat,
  38.         Number:  371,
  39.     }
  40.     amerikanu := grupa{
  41.         Country: "United States",
  42.         Words:   usa,
  43.         Number:  1,
  44.     }
  45.     data := []grupa{latviesu, amerikanu}
  46.     tpl.ExecuteTemplate(res, "one.gohtml", data)
  47. }
  48. ========================================
  49. <html>
  50. <body>
  51.     {{range $index, $element := .}}
  52.         <li>{{$index}} - {{$element}}</li>
  53.     {{end}}
  54. </body>
  55. </html>
  56. <html>
  57. <body>
  58.     {{range .}}
  59.         <b>{{.Country}} - {{.Number}}</b>
  60.         <li>{{.Words}}</li>
  61.         <li>{{index .Words 2}}, {{index .Words 1}}, {{index .Words 0}}</li>
  62.     {{end}}
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement