1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package main
- import (
- "fmt"
- "html/template"
- "net/http"
- )
- type User struct {
- ID int
- Name string
- Active bool
- }
- func (u *User) PrintActive() string {
- if !u.Active {
- return ""
- }
- return "method says user " + u.Name + " active"
- }
- func main() {
- tmpl, err := template.
- New("").
- ParseFiles("method.html")
- if err != nil {
- panic(err)
- }
- users := []User{
- User{1, "Vasily", true},
- User{2, "Ivan", false},
- User{3, "Dmitry", true},
- }
- http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- err := tmpl.ExecuteTemplate(w, "method.html",
- struct {
- Users []User
- }{
- users,
- })
- if err != nil {
- panic(err)
- }
- })
- fmt.Println("starting server at :8080")
- http.ListenAndServe(":8080", nil)
- }
|