| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | ) |
| 7 | |
| 8 | var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 9 | w.Write([]byte("hello world")) |
| 10 | }) |
| 11 | |
| 12 | type T1 struct { |
| 13 | Name string |
| 14 | } |
| 15 | |
| 16 | func (t *T1) Handler(h http.Handler) http.Handler { |
| 17 | fmt.Println("#1", t.Name) |
| 18 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | fmt.Println("#2", t.Name) |
| 20 | h.ServeHTTP(w, r) |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | func main() { |
| 25 | t := &T1{"myName"} |
| 26 | handler := t.Handler(myHandler) |
| 27 | http.ListenAndServe(":8080", handler) |
| 28 | } |