我正在學習 go 并且我有以下代碼可以正常工作:resp, err := http.Get(url) // get the html ...doc, err := html.Parse(resp.Body) // parse the html page現在我想先打印出 html 然后進行解析:resp, err := http.Get(url) ...b, err := ioutil.ReadAll(resp.Body) // this line is added, not working now...doc, err := html.Parse(resp.Body)我猜原因是 resp.Body 是一個 reader,我不能調用 read 兩次?知道如何正確執行此操作嗎?復制resp.Body?
1 回答

森欄
TA貢獻1810條經驗 獲得超5個贊
由于客戶端從網絡流式傳輸響應正文,因此不可能兩次讀取正文。
閱讀對 a 的響應,[]byte就像您已經在做的那樣。io.Reader使用bytes.NewReader為 HTML 解析器創建一個字節。
resp, err := http.Get(url)
...
b, err := ioutil.ReadAll(resp.Body)
doc, err := html.Parse(bytes.NewReader(b))
- 1 回答
- 0 關注
- 192 瀏覽
添加回答
舉報
0/150
提交
取消