纽约时报发布了他们的 Go的gzip中间件包 。
你刚过去了 http.HandlerFunc 通过他们的 GzipHandler 你完成了它看起来像这样:
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) }
现在有开箱即用的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