这是一个人为的例子。为什么 .Read 方法将响应正文的字节读取到我的字节切片中?“读取读取最多 len(p) 字节到 p。它返回读取的字节数(0 <= n <= len(p))和遇到的任何错误。为什么此请求不将任何数据读取到 p 中?func Fetch(){ req, err := http.NewRequest("GET", "https://swapi.dev/api/people/1",nil) if err != nil { log.Println(err) return } req.Close = true // closes the request once the request has been made // Transport caches connections for future use var transport = &http.Transport{DisableKeepAlives: true} var myClient = &http.Client{Timeout: 10 * time.Second, Transport: transport} resp, err := myClient.Do(req) p := []byte{} a1, err := resp.Body.Read(p) // a, err := ioutil.ReadAll(resp.Body) // this works fmt.Println(a1) fmt.Println(err) fmt.Println(p)}
- 1 回答
- 0 关注
- 137 浏览
添加回答
举报
0/150
提交
取消
