나는 가기가 좋다. 하지만 REST API를 가지고 놀고 있습니다. 난 내가 http 응답 텍스트에 gzip 압축
내가 내 응답 gzip을이 기능을 사용하고자이 두 가지 기능에 json.Encoder로 json.Marshal에서 같은 동작을 얻을 캔트 :func gzipFast(a *[]byte) []byte {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
defer gz.Close()
if _, err := gz.Write(*a); err != nil {
panic(err)
}
return b.Bytes()
}
을하지만이 기능이 반환
내가 주석을 해제 할 때func CompressedGet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
box := Box{Width: 10, Height: 20, Color: "gree", Open: false}
box.ars = make([]int, 100)
for i := range box.ars {
box.ars[i] = i
}
//fmt.Println(r.Header.Get("Content-Encoding"))
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")
b, _ := json.Marshal(box)
//fmt.Println(len(b))
//fmt.Println(len(gzipFast(&b)))
fmt.Fprint(w, gzipFast(&b))
//fmt.Println(len(gzipSlow(b)))
//gz := gzip.NewWriter(w)
//defer gz.Close()
//json.NewEncoder(gz).Encode(box)
r.Body.Close()
}
:하지만 여기
curl http://localhost:8081/compressedget --compressed --verbose
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8081 (#0)
> GET /compressedget HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.47.0
> Accept: */*
> Accept-Encoding: deflate, gzip
>
< HTTP/1.1 200 OK
< Content-Encoding: gzip
< Content-Type: application/json
< Date: Wed, 24 Aug 2016 00:59:38 GMT
< Content-Length: 30
<
* Connection #0 to host localhost left intact
는 이동 기능입니다
//gz := gzip.NewWriter(w)
//defer gz.Close()
//json.NewEncoder(gz).Encode(box)
잘 작동합니다.
3 줄이 필요하지 않을 수 있습니다. gzipFast 함수는 이미 json으로 인코딩 된 상자의 gzipping을 수행하고 있습니다. – Sridhar