go - 如何在没有第三方库的情况下在 Go 1.8 中设置 http2 服务器?

标签 go

听说最新的 Go 版本将支持 http2。如何在不使用 golang.org/x/net/http2 的情况下搭建 http2 服务器?

在以前的版本中,您可以执行以下操作:

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"))
}

最佳答案

在大多数情况下,您只需使用 net/http:

Starting with Go 1.6, the http package has transparent support for the HTTP/2 protocol when using HTTPS.

The http package's Transport and Server both automatically enable HTTP/2 support for simple configurations. 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. 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.

关于go - 如何在没有第三方库的情况下在 Go 1.8 中设置 http2 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798419/

相关文章:

go - Go中的生物识别登录(webauthn),如何验证签名

golang diff类型的函数提供 "unresolved type”

pointers - Go Pointers - 通过指针将值附加到 slice

go - 如何将 web 模板变量设置为动态 html 和 golang 代码?

unit-testing - 如何测试来自Playground/Validator的验证错误?

node.js - golang base64 编码 vs nodejs 缓冲区 base64 编码

c++ - 戈朗 : call Windows DLL functions

GO 语言 : Reading a file and turning the content into an array

rest - Go Gin 设置和从中间件访问上下文值

go - 是否可以有多个FileServer处理程序从不同的文件夹返回?