Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.33 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "html/template"
  5.     "net/http"
  6. )
  7.  
  8. var tpl *template.Template
  9.  
  10. func init() {
  11.     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  12. }
  13.  
  14. type grupa struct {
  15.     Country string
  16.     Words   []string
  17.     Number  int
  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.     mappy := map[int]string{
  29.         1: "viens",
  30.         2: "divi",
  31.         3: "tris",
  32.         4: "cetri",
  33.     }
  34.     tpl.ExecuteTemplate(res, "index.gohtml", mappy)
  35. }
  36.  
  37. func one(res http.ResponseWriter, req *http.Request) {
  38.     lat := []string{"janis", "karlsi", "edgars"}
  39.     usa := []string{"john", "carl", "eddy"}
  40.     latviesu := grupa{
  41.         Country: "Latvia",
  42.         Words:   lat,
  43.         Number:  371,
  44.     }
  45.     amerikanu := grupa{
  46.         Country: "United States",
  47.         Words:   usa,
  48.         Number:  1,
  49.     }
  50.     data := []grupa{latviesu, amerikanu}
  51.     tpl.ExecuteTemplate(res, "one.gohtml", data)
  52. }
  53. =================================
  54. <html>
  55. <body>
  56.     {{range $cipars, $vards := .}}
  57.         <li>{{$cipars}} ir {{$vards}}.</li>
  58.     {{end}}
  59. </body>
  60. </html>
  61. <html>
  62. <body>
  63.     {{range .}}
  64.         <b>{{.Country}} - {{.Number}}</b>
  65.         <li>{{.Words}}</li>
  66.         <li>{{index .Words 2}}, {{index .Words 1}}, {{index .Words 0}}</li>
  67.     {{end}}
  68. </body>
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement