dsljpwi494719 2017-03-14 23:28
浏览 17
已采纳

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

I heard that http2 would be supported in the latest Go versions. How can I put up a http2 server without using 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"))
}
  • 写回答

1条回答 默认 最新

  • dongtuo5611 2017-03-14 23:32
    关注

    You just use net/http in most instances:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题