如何在Golang中开发天气查询工具_调用第三方API获取数据

Go天气查询工具需调用OpenWeatherMap等API,用net/http和encoding/json发起请求并解析JSON响应,定义匹配结构体,处理错误与边界情况,支持命令行参数及环境变量,建议添加超时控制。

在 Go 语言中开发天气查询工具,核心是调用可靠的第三方天气 API(如 OpenWeatherMap、WeatherAPI 或 AccuWeather),并用 Go 的标准库(net/httpencoding/json)完成请求与解析。关键在于正确构造请求、处理响应、结构化解析 JSON,并做好错误和边界情况处理。

选择并注册合适的天气 API

推荐初学者使用 OpenWeatherMap(免费版支持 1000 次/天,无需信用卡):

  • 访问 https://www./link/a5aef5357bc5d38853c0b0d3cca7bf8c 注册账号,获取免费 API Key
  • 常用接口:https://api.openweathermap.org/data/2.5/weather?q={city}&appid={key}&units=metric(支持城市名、坐标、IP 等多种查询方式)
  • 注意单位参数:units=metric(摄氏)、units=imperial(华氏)、units=kelvin

定义结构体匹配 API 返回的 JSON

OpenWeatherMap 的响应字段较固定,需按实际 JSON 结构定义 Go struct,首字母大写导出,字段加 json: 标签:

type WeatherResponse struct {
    Name string `json:"name"`
    Main struct {
        Temp     float64 `json:"temp"`
        FeelsLike float64 `json:"feels_like"`
        Humidity int     `json:"humidity"`
    } `json:"main"`
    Weather []struct {
        Main        string `json:"main"`
        Description string `json:"description"`
    } `json:"weather"`
    Sys struct {
        Country string `json:"country"`
    } `json:"sys"`
}

可借助工具如 json-to-go 快速生成结构体。

发起 HTTP 请求并解析响应

使用 net/http 发起 GET 请求,检查状态码,再用 json.Unmarshal 解析:

func GetWeather(city, apiKey string) (*WeatherResponse, error) {
    url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric", 
        url.PathEscape(city), apiKey)
resp, err := http.Get(url)
if err != nil {
    return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
    body, _ := io.ReadAll(resp.Body)
    return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(body))
}

var data WeatherResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
    return nil, fmt.Errorf("JSON decode failed: %w", err)
}

return &data, nil

}

注意:url.PathEscape(city) 防止城市名含空格或特殊字符导致请求失败;务必检查 StatusCode 再解码。

封装命令行交互与错误提示

flag 包读取命令行参数,提升可用性:

func main() {
    city := flag.String("city", "", "城市名称,例如:Beijing")
    apiKey := flag.String("key", os.Getenv("OWM_API_KEY"), "OpenWeatherMap API Key")
    flag.Parse()
if *city == "" {
    log.Fatal("请通过 -city 指定城市")
}
if *apiKey == "" {
    log.Fatal("请通过 -key 指定 API Key,或设置环境变量 OWM_API_KEY")
}

weather, err := GetWeather(*city, *apiKey)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("? %s, %s\n", weather.Name, weather.Sys.Country)
fmt.Printf("?️  温度: %.1f°C (体感 %.1f°C)\n", weather.Main.Temp, weather.Main.FeelsLike)
fmt.Printf("? 湿度: %d%%\n", weather.Main.Humidity)
fmt.Printf("☁️  天气: %s (%s)\n", weather.Weather[0].Main, weather.Weather[0].Description)

}

运行示例:go run main.go -city="Shanghai" -key="your_api_key",也可将 key 存入环境变量避免明文暴露。

不复杂但容易忽略的是:超时控制、重试机制、API 频率限制提醒和中文城市名编码(部分 API 要求 UTF-8 + URL 编码)。生产环境建议加上 http.Client{Timeout: 10 * time.Second}