type Handler interface { ServeHTTP(ResponseWriter, *Request) }
处理http请求的接口---相当于Java的Controller方法
配合http包下的方法使用
func ListenAndServe(addr string, handler Handler) error { server := &Server{Addr: addr, Handler: handler} return server.ListenAndServe() }
案例:
func (db Database) ServeHTTP(w http.ResponseWriter, req *http.Request) { log.Println(req.URL) for item, price := range db { fmt.Fprintf(w, "%s: %s\n", item, price) } }
db := myhttp.Database{"shoes": 50, "socks": 5} http.ListenAndServe("localhost:8000", db)
标签:http,addr,db,接口,ListenAndServe,Handler From: https://www.cnblogs.com/sangejin/p/17932285.html