```go
package main
import(
"fmt"
"net/http"
"compress/gzip"
)
func main() {
url := "https://blog.csdn.net/yjp19871013/article/details/82555417"
client := &http.Client {};
req, err := http.NewRequest("GET", url, nil);
if (nil != err) {
return;
}
req.Header.Add("Accept-Encoding", "gzip, deflate");
resp, err := client.Do(req)
if (nil != err) {
return;
}
defer resp.Body.Close();
if 200 == resp.StatusCode {
a := resp.Header
for k,
v := range a {
fmt.Println(k, v)
}
fmt.Println(resp.Header.Get("Content-Encoding"));
reader, _ := gzip.NewReader(resp.Body)
for {
buf := make([] byte, 1024)
n,err := reader.Read(buf)
if nil != err {
return;
}
if 0 == n {
return ;
}
fmt.Println(string(buf))
}
} else {
fmt.Println(resp.StatusCode)
}
}
```
io.Reader接口说明里面讲的很清楚,Read会返回n>0且err=EOF的情况,即读取最后不满buffer长度的数据场景,修改成如下即可。
```go
for {
buf := make([] byte, 1024)
n,err := reader.Read(buf)
if n > 0 {
fmt.Println(string(buf))
}
if nil != err {
return;
}
}
```
#1
更多评论
看来楼主有些写法还在用C语言的习惯啊。不过从io.Reader里面读出全部数据还是建议使用byt, err := ioutil.ReadAll(reader)
如果坚持自己写方法的话可以参考一楼回答。我对楼主的代码做了写更改,就是把一些写法简化,去掉分号。
```go
package main
import (
"compress/gzip"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "https://blog.csdn.net/yjp19871013/article/details/82555417"
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if nil != err {
return
}
req.Header.Add("Accept-Encoding", "gzip, deflate")
resp, err := client.Do(req)
if nil != err {
return
}
defer resp.Body.Close()
if 200 == resp.StatusCode {
a := resp.Header
for k, v := range a {
fmt.Println(k, v)
}
fmt.Println(resp.Header.Get("Content-Encoding"))
reader, err := gzip.NewReader(resp.Body)
if nil != err {
return
}
byt, err := ioutil.ReadAll(reader)
if nil != err {
return
}
fmt.Println(string(byt))
} else {
fmt.Println(resp.StatusCode)
}
}
```
#2