🐹 Go (Golang) Intermediate

How do you build an HTTP server in Go using net/http?

Answer

Go's standard library net/http package makes building HTTP servers straightforward. Register handlers with http.HandleFunc("/path", handler) where a handler is func(w http.ResponseWriter, r *http.Request). Start the server with http.ListenAndServe(":8080", nil). For more control, create a custom http.ServeMux (router) and an http.Server struct with configurable timeouts (always set ReadTimeout, WriteTimeout, IdleTimeout to prevent resource exhaustion). For production services, most teams use a lightweight router like chi or gorilla/mux on top of the standard server.