Quiero saber qué está pasando aquí.Función que implementa la interfaz
No es la interfaz para un controlador HTTP:
type Handler interface {
ServeHTTP(*Conn, *Request)
}
Esta implementación Creo que entiendo.
type Counter int
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
fmt.Fprintf(c, "counter = %d\n", ctr);
ctr++;
}
Según mi entender es que el tipo "Contador" implementa la interfaz ya que tiene un método que tiene la firma requerida. Hasta aquí todo bien. Entonces se da este ejemplo:
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);
¿Puede alguien explicar por qué o cómo encajan estas varias funciones juntas?
Ok, creo que lo entiendo ahora, lo que me hizo tropezar fue la conversión de notFound a HandlerFunc. Después de releer la sección de conversiones de go efectivo, queda más claro cómo eso también puede aplicarse a las funciones. http://golang.org/doc/effective_go.html#conversions – mbarkhau