go语言中的http.Handle和http.HandleFunc
go的http库中Handle和HandleFunc的第一个参数都是pattern,即请求路径,区别在于第二参数。
Handle
func Handle(pattern string, handler Handler) {
DefaultServeMux.Handle(pattern, handler) }
Handle的第二个参数是接口类型,该接口只有一个方法:
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
因此我们需要自定义结构体和ServeHTTP方法以实现该接口。例如