Function implementing interface

前端 未结 2 922
终归单人心
终归单人心 2021-02-02 15:55

I want to know what is happening here.

There is the interface for a http handler:

type Handler interface {
    ServeHTTP(*Conn, *Request)
}
2条回答
  •  独厮守ぢ
    2021-02-02 16:37

    What exactly don't you understand about the second half? It's the same pattern as above. Instead of defining the Counter type as an int, they define a function called notFound. They then create a type of function called HandlerFunc that takes two parameters, a connection and a request. they then create a new method called ServeHTTP, that gets bound to the HandlerFunc type. Handle404 is simply an instance of this class that uses the notFound function.

提交回复
热议问题