ここで何が起こっているのか知りたいです。
httpハンドラーのインターフェースは次のとおりです。
type Handler interface {
ServeHTTP(*Conn, *Request)
}
この実装は私が理解していると思います。
type Counter int
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
fmt.Fprintf(c, "counter = %d\n", ctr);
ctr++;
}
私の理解では、タイプ「Counter」は必要な署名を持つメソッドを持っているため、インターフェースを実装しているということです。ここまでは順調ですね。次に、この例を示します。
func notFound(c *Conn, req *Request) {
c.SetHeader("Content-Type", "text/plain;", "charset=utf-8");
c.WriteHeader(StatusNotFound);
c.WriteString("404 page not found\n");
}
// Now we define a type to implement ServeHTTP:
type HandlerFunc func(*Conn, *Request)
func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
f(c, req) // the receiver's a func; call it
}
// Convert function to attach method, implement the interface:
var Handle404 = HandlerFunc(notFound);
誰かがこれらのさまざまな機能がなぜまたはどのように組み合わされるのかについて詳しく説明できますか?