ExpertiDict

Go · Intermediário · Inglês · Técnico

http.Handler

/eɪtʃ.ti.ti.ˈpi ˈhænd.lɚ/interface

Interface do net/http com um método, ServeHTTP(ResponseWriter, *Request). Qualquer tipo que a implemente trata requisições, e é a peça central de roteadores e middlewares.

Onde usar

Servidores e frameworks Go giram em torno dela. Middleware é uma função que recebe um Handler e devolve outro, embrulhando o original.

Em código

type Ola struct{}

func (Ola) ServeHTTP(w http.ResponseWriter,
  r *http.Request) {
  fmt.Fprintln(w, "olá")
}
// http.ListenAndServe(":8080", Ola{})

Exemplos

Any type with a ServeHTTP method is an http.Handler.

Qualquer tipo com um método ServeHTTP é um http.Handler.

The logging middleware wraps another handler.

O middleware de log embrulha outro handler.

Cuidado

Não confunda Handler, a interface, com HandlerFunc, o tipo que adapta uma função comum para satisfazê-la.

Sinônimos

handler HTTP

Veja também

Todos os termos de Go