下面代码将报错,提示panic: runtime error: invalid memory address or nil pointer dereference

resp, _ := http.Get(link.ImgUrl)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

正确的应是, defer resp.Body.Close() 应在 ioutil.ReadAll之后

resp, _ := http.Get(link.ImgUrl)
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

菜鸟总结 0.0

来源:中战博客,欢迎分享!
标签: golang,http,panic