HowtoGo
Home / Standard Library / net/http
Standard Library

net/http

net/http is a full HTTP client and server, no framework required.

Examples

Bare ListenAndServe sets no timeouts. Configure a *Server.

mux := http.NewServeMux()
mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    w.Write([]byte("{\"status\":\"ok\"}"))
})

server := &http.Server{
    Addr:         ":8080",
    Handler:      mux,
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 10 * time.Second,
    IdleTimeout:  120 * time.Second,
}
log.Fatal(server.ListenAndServe())
Output
$ curl localhost:8080/health
{"status":"ok"}
FunctionDescription
NewServeMux() *ServeMux
mux := http.NewServeMux()
Empty router. Register routes on the returned value.
(*ServeMux) HandleFunc(pattern string, fn func(ResponseWriter, *Request))
mux.HandleFunc("GET /health", h)
Go 1.22+ patterns can include a method: "GET /path".
ListenAndServe(addr string, handler Handler) error
log.Fatal(http.ListenAndServe(":8080", mux))
Blocks. Error only on shutdown.
(*Server) ListenAndServe() error
log.Fatal(server.ListenAndServe())
Same, on a *Server you configured yourself.
Error(w ResponseWriter, msg string, code int)
http.Error(w, "bad request", 400)
Writes status + message directly to w.
Get(url string) (*Response, error)
resp, err := http.Get(u)
defer resp.Body.Close() once err is nil.
NewRequest(method, url string, body io.Reader) (*Request, error)
req, err := http.NewRequest("GET", u, nil)
For use with a custom *Client.
Handler interface { ServeHTTP(ResponseWriter, *Request) }
func (h) ServeHTTP(w, r)
Implement this to be usable as a Handler.
Hijacker interface { Hijack() (net.Conn, *bufio.ReadWriter, error) }
conn, bufrw, err := hj.Hijack()
Type-assert w to get the raw connection.