我想解析一个Web请求的响应,但是我很难以字符串形式访问它.
func main() {
resp, err := http.Get("http://google.hu/")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
ioutil.WriteFile("dump", body, 0600)
for i:= 0; i < len(body); i++ {
fmt.Println( body[i] ) // This logs uint8 and prints numbers
}
fmt.Println( reflect.TypeOf(body) )
fmt.Println("done")
}
Run Code Online (Sandbox Code Playgroud)
如何以字符串形式访问响应?ioutil.WriteFile正确写入对文件的响应.
我已经检查了包参考,但它并没有真正帮助.
有没有人对Golang的NopCloser功能有任何好处或解释?
我环顾四周,但除了Golang的主要文档解释之外没有找到任何东西:
NopCloser返回一个ReadCloser,其中包含一个无操作的Close方法,用于包装提供的Reader r.
任何指针或解释将不胜感激.谢谢.