[英]How can I set up a http2 server in Go 1.8 without third party libraries?
I heard that http2 would be supported in the latest Go versions. 我听说最新的Go版本将支持http2。 How can I put up a http2 server without using
golang.org/x/net/http2
? 我怎样才能把一个http2服务器,而不使用
golang.org/x/net/http2
?
In previous versions you could do something like this: 在以前的版本中,您可以执行以下操作:
package main
import (
"log"
"net/http"
"os"
"golang.org/x/net/http2"
)
func main() {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
srv := &http.Server{
Addr: ":443",
Handler: http.FileServer(http.Dir(cwd)),
}
http2.ConfigureServer(srv, &http2.Server{})
log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
}
You just use net/http
in most instances: 在大多数情况下,您只使用
net/http
:
Starting with Go 1.6, the http package has transparent support for the HTTP/2 protocol when using HTTPS.
从Go 1.6开始,使用HTTPS时,http软件包对HTTP / 2协议具有透明支持。
The http package's Transport and Server both automatically enable HTTP/2 support for simple configurations.
http包的Transport和Server都自动为简单配置启用HTTP / 2支持。 To enable HTTP/2 for more complex configurations, to use lower-level HTTP/2 features, or to use a newer version of Go's http2 package, import "golang.org/x/net/http2" directly and use its ConfigureTransport and/or ConfigureServer functions.
要为更复杂的配置启用HTTP / 2,使用较低级别的HTTP / 2功能或使用Go的http2软件包的较新版本,请直接导入“ golang.org/x/net/http2”并使用其ConfigureTransport和/或ConfigureServer功能。 Manually configuring HTTP/2 via the golang.org/x/net/http2 package takes precedence over the net/http package's built-in HTTP/2 support.
通过golang.org/x/net/http2软件包手动配置HTTP / 2优先于net / http软件包的内置HTTP / 2支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.