如何在 Go 中正确发起 HTTP GET 请求并解析 JSON 响应

如何在 Go 中正确发起 HTTP GET 请求并解析 JSON 响应

本文详解 go 语言中发起 http 请求、读取响应体并安全解析 json 的完整流程,涵盖错误处理、资源释放和结构化解析等关键实践。

在 Go 中,http.Get() 仅返回 *http.Response 结构体(包含状态码、Header 等元信息),并不会自动读取或解析响应体内容。你看到的 &{200 OK …} 输出正是 resp 变量本身的指针打印结果,而非 JSON 数据——真正的 JSON 内容位于 resp.Body 这个 io.ReadCloser 流中,必须显式读取并解码。

以下是修正后的标准实践代码(已补充必要导入和完整示例):

package main  import (     "encoding/json"     "fmt"     "io"     "net/http" )  func getDuckDuckGo(keyword string) (map[string]interface{}, error) {     // 1. 发起请求并检查错误     resp, err := http.Get("https://api.duckduckgo.com/?q=" + keyword + "&format=json&no_html=1")     if err != nil {         return nil, fmt.Errorf("HTTP request failed: %w", err)     }     defer resp.Body.Close() // 2. 必须关闭响应体,防止连接泄漏      // 3. 检查 HTTP 状态码     if resp.StatusCode != http.StatusOK {         return nil, fmt.Errorf("API returned non-200 status: %d", resp.StatusCode)     }      // 4. 使用 json.Decoder 直接从 Body 解码(流式、内存友好)     var result map[string]interface{}     if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {         return nil, fmt.Errorf("JSON decode failed: %w", err)     }      return result, nil }  func main() {     data, err := getDuckDuckGo("food")     if err != nil {         panic(err)     }      // 示例:安全提取字段(注意类型断言)     if definition, ok := data["AbstractText"].(string); ok && definition != "" {         fmt.Println("Definition:", definition)     } else {         fmt.Println("No definition found.")     }      if results, ok := data["RelatedTopics"].([]interface{}); ok && len(results) > 0 {         if first, ok := results[0].(map[string]interface{}); ok {             if firstText, ok := first["Text"].(string); ok {                 fmt.Println("First related topic:", firstText)             }         }     } }

⚠️ 关键注意事项:

  • 永远检查 http.Get() 和 json.Decode() 的错误:忽略错误是 Go 程序崩溃或静默失败的主因;
  • 务必调用 resp.Body.Close():即使使用 defer,也应在 if err != nil 后立即 return,避免执行到 defer 前 panic;
  • 优先使用 json.Decoder 而非 json.Unmarshal(io.ReadAll(…)):前者支持流式解析,对大响应更省内存;
  • DuckDuckGo API 已弃用 HTTP(需改用 HTTPS),且建议添加 &no_html=1 避免 HTML 转义干扰;
  • 若需强类型保障,应定义结构体(如 type DuckDuckResponse struct { AbstractText stringjson:”AbstractText”}),而非泛型 map[string]interface{}。

掌握这一模式,即可稳健处理任意 RESTful JSON API,为构建爬虫、微服务客户端等打下坚实基础。