Ric*_*777 40 webserver http go
我开始在Go中编写服务器端应用程序.我想使用Accept-Encoding请求标头来确定是否GZIP响应实体.我曾希望使用http.Serve或http.ServeFile方法直接找到一种方法.
这是一个非常普遍的要求; 我错过了什么或者我需要推出自己的解决方案吗?
Mos*_*vah 23
你只需要http.HandlerFunc
通过他们GzipHandler
,你就完成了.它看起来像这样:
package main
import (
"io"
"net/http"
"github.com/nytimes/gziphandler"
)
func main() {
withoutGz := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "Hello, World")
})
withGz := gziphandler.GzipHandler(withoutGz)
http.Handle("/", withGz)
http.ListenAndServe("0.0.0.0:8000", nil)
}
Run Code Online (Sandbox Code Playgroud)
rpu*_*kar 22
现在有开箱即用的gzip支持,请参阅链接获取更多信息:https://golang.org/pkg/compress/gzip/
目前还没有对gzip压缩的HTTP响应提供"开箱即用"的支持.但添加它是非常微不足道的.看一下
https://gist.github.com/the42/1956518
也
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/cgUp8_ATNtc
归档时间: |
|
查看次数: |
21313 次 |
最近记录: |