Can someone please explain http.HandlerFunc() ?

106 visualizzazioni
Passa al primo messaggio da leggere

joe mcguckin

da leggere,
3 apr 2020, 22:11:0103/04/20
a golang-nuts
I see some code like:


r.NotFoundHandler =http.HandlerFunc(notfound)

Where notfound() has the signature of

func notfound(w http.ResponseWriter, r *http.Request)

What is HandlerFunc() doing? It’s not a function. Is it some sort of type coercion or casting?

I can see how casting that might work with simple values (e.g.numbers,integers, etc) but functions?

The web docs say:
The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

If notfound() is ' func notfound(w http.ResponseWriter, r *http.Request)’, what is it being converted into?

Why is this needed?

Thanks,

Joe

Kurtis Rader

da leggere,
3 apr 2020, 22:28:3303/04/20
a joe mcguckin, golang-nuts
On Fri, Apr 3, 2020 at 7:10 PM joe mcguckin <j...@via.net> wrote:
I see some code like:

          r.NotFoundHandler =http.HandlerFunc(notfound)

Where notfound() has the signature of

          func notfound(w http.ResponseWriter, r *http.Request)

What is HandlerFunc() doing? It’s not a function. Is it some sort of type coercion or casting?
I can see how casting that might work with simple values (e.g.numbers,integers, etc)  but functions?
 

I'm guessing you've never used a language where functions are first-class objects that can be used indirectly; as opposed to direct calls you can search for using your editor/IDE. As the documentation states, it is an adapter that converts one function type into another.

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Tamás Gulácsi

da leggere,
4 apr 2020, 03:09:5504/04/20
a golang-nuts
And most importantly (here) functions can have methods ( as any type), so HandlerFunc implements the Handler interface.

Brian Candler

da leggere,
4 apr 2020, 05:20:4304/04/20
a golang-nuts
Sorry, but I believe that explanation is wrong.

http.HandlerFunc is a type, not a function:

And so the code shown by the OP is indeed performing a type conversion, to a compatible type which is decorated with methods.

You can do the same with plain types like "int": see
A type of "function taking X returning Y" is no different in this regard.

So whilst it's true that Go supports functions returning functions, this is not what's happening here.

Jesper Louis Andersen

da leggere,
4 apr 2020, 06:07:0204/04/20
a joe mcguckin, golang-nuts
The function `notfound` is a plain function. It has a specific signature, (w http.ResponseWriter, r *http.Request). There is a type, HandlerFunc, with the following specification:

type HandlerFunc func(ResponseWriter, *Request)

Types in Go are "generative", so the HandlerFunc type is different from a plain function. But if we wrap it, like your code does, we "lift" the plain function into becoming a HandlerFunc. Now, why do we do that? Well, HandlerFunc implements the Handler interface:

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

And the http server uses the Handler interface all over the place. This allows us to "adapt" any function with the right signature into an http.Handler. Had we just passed the plain function, the trouble is that it doesn't implement the ServeHTTP method. And we need this for the Handler interface. 

In short, we can "tag" a function as being an http.Handler and use it as one in our web server by adapting through the HandlerFunc type.

Other people might have comments here as to my specificity wrt the explanation.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/C594C073-3DF8-4CD1-8C43-59AE59AADF92%40via.net.


--
J.
Rispondi a tutti
Rispondi all'autore
Inoltra
0 nuovi messaggi