這是一個人為的例子。為什么 .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 關注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消
