Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
如何在Golang中读取网络响应内容_Golang 网络响应读取实践_创想鸟

如何在Golang中读取网络响应内容_Golang 网络响应读取实践

答案:在Golang中读取网络响应需正确处理resp.Body并及时关闭。小数据可用io.ReadAll一次性读取;文本流适合bufio.Scanner逐行处理;JSON响应推荐json.NewDecoder直接解码;无论何种方式都必须defer resp.Body.Close(),并检查StatusCode与设置超时,确保资源释放与程序健壮性。

如何在golang中读取网络响应内容_golang 网络响应读取实践

在Golang中读取网络响应内容,通常是在发送HTTP请求后从*http.Response中获取返回的数据。关键在于正确处理响应体(Body)并及时关闭资源,避免内存泄漏。下面介绍几种常见的实践方式。

使用ioutil.ReadAll一次性读取

最简单的方式是使用ioutil.ReadAll将响应体全部读入内存。适用于响应数据较小的场景。

注意:Go 1.16之后推荐使用io.ReadAll替代ioutil.ReadAll。大响应体可能导致内存占用过高。

示例代码:

resp, err := http.Get("https://api.example.com/data")if err != nil {    log.Fatal(err)}defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)if err != nil {log.Fatal(err)}fmt.Println(string(body))

使用bufio逐行读取

当响应是文本格式(如日志流、JSON行等),可使用bufio.Scanner逐行处理,节省内存。

立即学习“go语言免费学习笔记(深入)”;

适合处理大型文本响应或流式数据。

示例:

resp, err := http.Get("https://api.example.com/stream")if err != nil {    log.Fatal(err)}defer resp.Body.Close()

scanner := bufio.NewScanner(resp.Body)for scanner.Scan() {fmt.Println(scanner.Text())}if err := scanner.Err(); err != nil {log.Fatal(err)}

直接解码JSON响应

如果响应是JSON格式,建议直接用json.NewDecoder解码,无需先读字符串。

这种方式更高效,尤其适合结构化数据。

示例:

type User struct {    Name string `json:"name"`    Age  int    `json:"age"`}

resp, err := http.Get("https://www.php.cn/link/d3edfaac7b134307771e928cc3293131")if err != nil {log.Fatal(err)}defer resp.Body.Close()

var user Userif err := json.NewDecoder(resp.Body).Decode(&user); err != nil {log.Fatal(err)}fmt.Printf("%+vn", user)

处理错误与资源释放

无论采用哪种读取方式,都必须确保resp.Body.Close()被调用,防止连接未释放。

常见注意事项:

即使请求失败(如404、500),只要返回了*http.Response,就需关闭Body。网络错误或超时应设置http.Client的超时时间。检查resp.StatusCode判断是否为预期响应。

增强版请求示例:

client := &http.Client{Timeout: 10 * time.Second}req, _ := http.NewRequest("GET", "https://api.example.com/data", nil)

resp, err := client.Do(req)if err != nil {log.Fatal(err)}defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {log.Fatalf("HTTP %d", resp.StatusCode)}

body, _ := io.ReadAll(resp.Body)fmt.Println(string(body))

基本上就这些。根据响应大小和数据格式选择合适的读取方式,始终记得关闭Body,保证程序健壮性。

以上就是如何在Golang中读取网络响应内容_Golang 网络响应读取实践的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1425244.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golang如何在Kubernetes中管理持久化存储_Golang PVC与存储管理实践
上一篇 2025年12月16日 19:24:59
如何用Golang测试API请求与响应_Golang API请求响应测试实践
下一篇 2025年12月16日 19:25:10

相关推荐

发表回复

登录后才能评论
关注微信