ListenAndServe starts an HTTP server with a given address and handler.
The handler is usually nil, which means to use [DefaultServeMux].
[Handle] and [HandleFunc] add handlers to [DefaultServeMux]:
The http package has transparent support for the HTTP/2 protocol.
[Server] and [DefaultTransport] automatically enable HTTP/2 support
when using HTTPS. [Transport] does not enable HTTP/2 by default.
To enable or disable support for HTTP/1, HTTP/2, and/or unencrypted HTTP/2,
see the [Server.Protocols] and [Transport.Protocols] configuration fields.
To configure advanced HTTP/2 features, see the [Server.HTTP2] and
[Transport.HTTP2] configuration fields.
Alternatively, the following GODEBUG settings are currently supported:
GODEBUG=http2client=0 # disable HTTP/2 client support GODEBUG=http2server=0 # disable HTTP/2 server support GODEBUG=http2debug=1 # enable verbose HTTP/2 debug logs GODEBUG=http2debug=2 # ... even more verbose, with frame dumps
The "omithttp2" build tag may be used to disable the HTTP/2 implementation
contained in the http package.
Package http provides HTTP client and server implementations.
[Get], [Head], [Post], and [PostForm] make HTTP (or HTTPS) requests:
The caller must close the response body when finished with it:
Clients and Transports
For control over HTTP client headers, redirect policy, and other settings, create a [Client]:
For control over proxies, TLS configuration, keep-alives, compression, and other settings, create a [Transport]:
Clients and Transports are safe for concurrent use by multiple goroutines and for efficiency should only be created once and re-used.
Servers
ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use [DefaultServeMux]. [Handle] and [HandleFunc] add handlers to [DefaultServeMux]:
More control over the server's behavior is available by creating a custom Server:
HTTP/2
The http package has transparent support for the HTTP/2 protocol.
[Server] and [DefaultTransport] automatically enable HTTP/2 support when using HTTPS. [Transport] does not enable HTTP/2 by default.
To enable or disable support for HTTP/1, HTTP/2, and/or unencrypted HTTP/2, see the [Server.Protocols] and [Transport.Protocols] configuration fields.
To configure advanced HTTP/2 features, see the [Server.HTTP2] and [Transport.HTTP2] configuration fields.
Alternatively, the following GODEBUG settings are currently supported:
The "omithttp2" build tag may be used to disable the HTTP/2 implementation contained in the http package.