Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.26 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. <!DOCTYPE html>
  50. <html>
  51. <head>
  52.     <title>Structs</title>
  53. </head>
  54. <body>
  55.     {{range .}}
  56.         <b>{{.Country}} - {{.Number}}</b>
  57.         <li>{{.Words}}</li>
  58.         <li>{{index .Words 2}}, {{index .Words 1}}, {{index .Words 0}}</li>
  59.     {{end}}
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement