golang ioutilReadAllrespBody 已弃用
在 Go 1.16 版本中,ioutil.ReadAll(resp.Body) 已被标记为过时(deprecated),因为它可能会导致内存问题。相反,应该使用io.ReadAll(resp.Body)。io.ReadAll 函数会自动处理错误,并且使用更好的内存管理策略。
示例代码:
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
resp, err := http.Get("https://www.example.com/")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
原文地址: https://www.cveoy.top/t/topic/hlQB 著作权归作者所有。请勿转载和采集!