Handler versus Handler Function
As you may have noticed, we used two different functions before, http.Handle
and http.HandleFunc
, both of which have a path as their first parameter, but which differ in terms of the second parameter. These two functions both ensure that a specific path is handled by a function. http.Handle
, however, expects http.Handler
to handle the path, while http.HandleFunc
expects a function to do the same.
As we've seen before, http.Handler
is any struct having a method with this signature:
ServeHTTP(w http.ResponseWriter, r *http.Request)
So, in both cases, there will always be a function with http.ResponseWriter
and *http.Request
as parameters that will handle the path. As to when one or the other might be chosen may just be a matter of personal preference in many cases, but it might be important, when creating a complex project, for example, to choose the right method. Doing so will ensure that the structure of the project is optimal. Different...