HiveBrain v1.2.0
Get Started
← Back to all entries
gotchagoMajor

Difference between http.Handle and http.HandleFunc?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
andbetweenhandledifferencehandlefunchttp

Problem

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

http.Handle("/foo", fooHandler)
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})


I'm having sort of a difficulty understanding the difference between Handle and HandleFunc and why two are needed. Can somebody try to explain to a new Gopher in clear words?

Solution

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 directory 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 2286 (as of today):

2286  // The HandlerFunc type is an adapter to allow the use of
  2287  // ordinary functions as HTTP handlers. If f is a function
  2288  // with the appropriate signature, HandlerFunc(f) is a
  2289  // [Handler] that calls f.
  2290  type HandlerFunc func(ResponseWriter, *Request)
  2291  
  2292  // ServeHTTP calls f(w, r).
  2293  func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  2294      f(w, r)
  2295  }


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

Code Snippets

2286  // The HandlerFunc type is an adapter to allow the use of
  2287  // ordinary functions as HTTP handlers. If f is a function
  2288  // with the appropriate signature, HandlerFunc(f) is a
  2289  // [Handler] that calls f.
  2290  type HandlerFunc func(ResponseWriter, *Request)
  2291  
  2292  // ServeHTTP calls f(w, r).
  2293  func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  2294      f(w, r)
  2295  }

Context

Stack Overflow Q#21957455, score: 74

Revisions (0)

No revisions yet.