HTTP/2 for Go
http
package in golang supports HTTP/2 protocols.
It’s automatically configured.
The features of HTTP/2
In order to understand the benefits of HTTP/2, this document, provided by google, is helpful. It supports a lot of features including followings.
- binary format communications
- streaming messages
- multiplexing
- server push
Some examples are written in this page, and I checked it so much.
Streaming
main.go is an example for server streaming, including a server and a client codes.
The important part is calling Flush
method, whose interface is http.Flusher
and implemented by http.ResponseWriter
.
When this method is used, the buffered data on the server is sent to the client.
Server push
Like http.Flusher
, there is an interface http.Pusher
, which is implemented by http.ResponseWriter
, and we can use it for server push.
A path, http method, and headers can be configured to get requests from clients.
However, as of March 2020, http.Client
in Go does not support server push, neither does curl
cli.
If clients do not support server push but servers try to do it, then http.ErrNotSupported
is made.
For the details of this in Go, there is a page in an official blog. The example of server code for server push in Go can be found in this page.
About examples in this page
In order to use HTTP/2, it seems TLS must be configured. I tried to find the way to configure HTTP servers without TLS, but I couldn’t find it.