Difference between http.Handle and http.HandleFunc?

后端 未结 3 559
陌清茗
陌清茗 2020-12-07 15:19

The Go docs have the following example for the http package:

http.Handle(\"/foo\", fooHandler)
http.HandleFunc(\"/bar\", func(w http.ResponseWriter, r *http.         


        
相关标签:
3条回答
  • 2020-12-07 15:58

    No It's Different. Let's Examine

    func Handle(pattern string, handler Handler) {
        DefaultServeMux.Handle(pattern, handler) 
    }
    

    handle expects us to pass a Handler. Handler is an interface

    type Handler interface {
        ServeHTTP(ResponseWriter, *Request)
    }
    

    if any type implements ServeHTTP(ResponseWriter, *Request) for example: myCustomHandler then we can pass it like Handle(pattern string, myCustomHandler).

    In the second scenario:

    HandleFunc(pattern string, func(w ResponseWriter, r *Request) {
        // do some stuff
    }
    

    HandleFunc expects a function where Handle expects a Handler interface.

    So, if you just want to pass a function then you can use http.HandleFunc(..). Like @David showed that behind the scenes it implements Handler interface by calling ServeHTTP.

    type HandlerFunc func(ResponseWriter, *Request)
    
    // ServeHTTP calls f(w, r).
    func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
        f(w, r)
    }
    
    0 讨论(0)
  • 2020-12-07 16:13

    Basically, the HTTP server's "mux" has a map of path -> handler interface

    Interfaces are used here, I assume, to allow you to implement complex path handlers that have state.

    For example the file server from the standard package is a struct that contains the root dir for file service and implements the handler interface.

    that said, for simple stuff, a func is easier and more clear. So they added a special generator so you can easily pass in a func.

    Take a look at: server.go

    from line: 1216 (as of today)

      1216  type HandlerFunc func(ResponseWriter, *Request)
      1217  
      1218  // ServeHTTP calls f(w, r).
      1219  func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
      1220      f(w, r)
      1221  }
    

    What they are doing is implementing the interface on a custom type (which happens to match the api of the interface) that just calls itself.

    0 讨论(0)
  • 2020-12-07 16:25

    Very correct answers so I won't say much but explain it in simple terms:

    Problem: I want to create an object (type) that responds to HTTP requests.

    Solution: use http.Handle and the object you create should implement ServeHTTP interface from the HTTP package.

    Problem: I want a function to respond to my HTTP request.

    Solution: Use http.HandleFunc for that and register your function with the HTTP server.

    0 讨论(0)
提交回复
热议问题