net/http Flashcards
1
Q
What function starts the default http server?
A
func ListenAndServer(addr string, handler Handler) error
2
Q
Write a simple handler?
A
f := func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf("Hello, world!\n") h := http.HandlerFunc(f) }
3
Q
What settings help with a client that’s slow to send it request to the server?
A
http.Server { ReadTimeout time.Duration }
4
Q
What would with a slow server handler?
A
Wrap it with the http.TimeoutHandler
http.TimeoutHandler(h http.Handler, dt time.Duration, msg string) http.Handler
5
Q
What does net/http provide to deal with multiple handlers?
A
The HTTP request multiplexer http.ServeMux
. The multiplexer is itself a http.Handler.
mux := http.NewServeMux() mux.HandleFunc("/1", handleOne) mux.HandleFunc("/2", handleTwo)
6
Q
A