func Handle(pattern string, handler Handler) // Handle 函数将pattern和对应的handler注册进DefaultServeMux func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) // HandleFunc registers the handler function for the given pattern in the DefaultServeMux // Handler // type Handler interface { ServeHTTP(ResponseWriter, *Request) } // HandlerFunc可以将一个普通的处理函数转化为Handler type HandlerFunc func(ResponseWriter, *Request)
HandleFunc仅接受一个func为参数,相对于简洁些。Handle则须要传入一个带有ServeHTTP的结构体,所以控制逻辑能够灵活些。
api
package main import ( "fmt" "log" "net/http" "sync" ) type countHandler struct { mu sync.Mutex // guards n n int } func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.mu.Lock() defer h.mu.Unlock() h.n++ fmt.Fprintf(w, "count is %d\n", h.n) } func main() { http.Handle("/count", new(countHandler)) log.Fatal(http.ListenAndServe(":8080", nil)) }
h1 := func(w http.ResponseWriter, _ *http.Request) { io.WriteString(w, "Hello from a HandleFunc #1!\n") } h2 := func(w http.ResponseWriter, _ *http.Request) { io.WriteString(w, "Hello from a HandleFunc #2!\n") } http.HandleFunc("/", h1) http.HandleFunc("/endpoint", h2) log.Fatal(http.ListenAndServe(":8080", nil))
// 监听TCP而后调用handler对应的Serve去处理请求。handler默认为nil,则使用DefaultServeMux func ListenAndServe(addr string, handler Handler) error
路由调度器,根据请求url调用handler去处理。实现了Handle,HandleFunc方法。
ServeMux实现了ServeHTTP所以也是一个Handler接口服务器
// 新建 func NewServeMux() *ServeMux // 方法 func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) func (mux *ServeMux) Handle(pattern string, handler Handler) func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) // mux := http.NewServeMux() mux.Handle("/api/", apiHandler{}) mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { // The "/" pattern matches everything, so we need to check // that we're at the root here. if req.URL.Path != "/" { http.NotFound(w, req) return } fmt.Fprintf(w, "Welcome to the home page!") })
type Server struct { Addr string // TCP address to listen on, ":http" if empty Handler Handler // handler to invoke, http.DefaultServeMux if nil TLSConfig *tls.Config ReadTimeout time.Duration ReadHeaderTimeout time.Duration WriteTimeout time.Duration IdleTimeout time.Duration MaxHeaderBytes int TLSNextProto map[string]func(*Server, *tls.Conn, Handler) ConnState func(net.Conn, ConnState) ErrorLog *log.Logger // contains filtered or unexported fields } // 方法 func (srv *Server) Close() error func (srv *Server) ListenAndServe() error func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error func (srv *Server) RegisterOnShutdown(f func()) func (srv *Server) Serve(l net.Listener) error func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error func (srv *Server) SetKeepAlivesEnabled(v bool) func (srv *Server) Shutdown(ctx context.Context) error
能够经过Server类型来改变默认的Server配置,如改变监听端口等。函数
func main(){ http.HandleFunc("/", index) server := &http.Server{ Addr: ":8000", ReadTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second, } server.ListenAndServe() } // 自定义的serverMux对象也能够传到server对象中。 func main() { mux := http.NewServeMux() mux.HandleFunc("/", index) server := &http.Server{ Addr: ":8000", ReadTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second, Handler: mux, } server.ListenAndServe() }