在Go语言中使用http库来获取返回数据可以使用http.Get方法。以下是一个简单的示例:

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	// 发送GET请求
	resp, err := http.Get("https://api.example.com/data")
	if err != nil {
		fmt.Println("请求失败:", err)
		return
	}
	defer resp.Body.Close()

	// 读取响应数据
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("读取数据失败:", err)
		return
	}

	// 打印响应数据
	fmt.Println(string(body))
}

在上面的示例中,我们使用http.Get方法发送一个GET请求,并将返回的响应保存在resp变量中。然后使用ioutil.ReadAll方法读取响应的Body数据,并将其打印出来。

请注意,这只是一个简单的示例,实际使用时可能需要处理更多的错误和异常情况

golang http库获取返回数据

原文地址: http://www.cveoy.top/t/topic/ilEu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录