可以使用net/http包来发送HTTP请求,并使用encoding/json包来解析JSON数据。以下是一个示例代码:

package main

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

type Quote struct {
	Quote string `json:"c"`
	Author string `json:"f"`
}

func main() {
	url := "https://yiyan.baidu.com/api.php"

	response, err := http.Get(url)
	if err != nil {
		fmt.Println("HTTP GET请求失败:", err)
		return
	}
	defer response.Body.Close()

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

	var quote Quote
	err = json.Unmarshal(body, &quote)
	if err != nil {
		fmt.Println("解析JSON失败:", err)
		return
	}

	fmt.Println("名言:", quote.Quote)
	fmt.Println("作者:", quote.Author)
}

运行以上代码,会发送HTTP GET请求到指定的URL,获取到JSON数据后,将其解析为Quote结构体,并输出名言和作者信息。

注意:上述代码省略了错误处理和一些其他的边界情况处理,实际应用中需要根据具体情况进行适当的处理

使用golang获取httpsyiyanbaiducomapiphp 接口内容并输出json

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

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