method.go 746 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net/http"
  6. )
  7. type User struct {
  8. ID int
  9. Name string
  10. Active bool
  11. }
  12. func (u *User) PrintActive() string {
  13. if !u.Active {
  14. return ""
  15. }
  16. return "method says user " + u.Name + " active"
  17. }
  18. func main() {
  19. tmpl, err := template.
  20. New("").
  21. ParseFiles("method.html")
  22. if err != nil {
  23. panic(err)
  24. }
  25. users := []User{
  26. User{1, "Vasily", true},
  27. User{2, "Ivan", false},
  28. User{3, "Dmitry", true},
  29. }
  30. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  31. err := tmpl.ExecuteTemplate(w, "method.html",
  32. struct {
  33. Users []User
  34. }{
  35. users,
  36. })
  37. if err != nil {
  38. panic(err)
  39. }
  40. })
  41. fmt.Println("starting server at :8080")
  42. http.ListenAndServe(":8080", nil)
  43. }