94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
|
|
// GoWeather
|
||
|
|
//
|
||
|
|
// Copyright (c) 2023 Helmut Kaczmarek <code@helmutkaczmarek.de>
|
||
|
|
//
|
||
|
|
// My first small attempt in the programming language Go.
|
||
|
|
//
|
||
|
|
// GoWeather is a tiny command line program that queries the current
|
||
|
|
// weather in a specified city via OpenWeatherMap's API and displays the
|
||
|
|
// result in the terminal window.
|
||
|
|
//
|
||
|
|
// This program is free software: you can redistribute it and/or modify
|
||
|
|
// it under the terms of the GNU General Public License as published by
|
||
|
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
|
// (at your option) any later version.
|
||
|
|
//
|
||
|
|
// This program is distributed in the hope that it will be useful,
|
||
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
// GNU General Public License for more details.
|
||
|
|
//
|
||
|
|
// You should have received a copy of the GNU General Public License
|
||
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Structure to store the weather data from the API
|
||
|
|
type WeatherData struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
Main struct {
|
||
|
|
Temp float64 `json:"temp"`
|
||
|
|
} `json:"main"`
|
||
|
|
Weather []struct {
|
||
|
|
Description string `json:"description"`
|
||
|
|
} `json:"weather"`
|
||
|
|
Cod int `json:"cod"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
// Put your OpenWeatherMap API key and city ID here
|
||
|
|
apiKey := "API_KEY"
|
||
|
|
city := "CITY_ID"
|
||
|
|
|
||
|
|
// Generate the URL for the API call
|
||
|
|
url := fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?id=%s&appid=%s&units=metric", city, apiKey)
|
||
|
|
|
||
|
|
// Send the API call and get the response
|
||
|
|
response, err := http.Get(url)
|
||
|
|
if err != nil {
|
||
|
|
fmt.Println("Error getting the data:", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer response.Body.Close()
|
||
|
|
|
||
|
|
// Decode the JSON response into the WeatherData structure
|
||
|
|
var data WeatherData
|
||
|
|
if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
|
||
|
|
fmt.Println("Error decoding data:", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check for API error codes and display error messages if there are any
|
||
|
|
if data.Cod == 401 {
|
||
|
|
fmt.Println("Error: Invalid API key.")
|
||
|
|
fmt.Println("API error message:", data.Message)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if data.Cod != 200 {
|
||
|
|
fmt.Println("Error: Request failed.")
|
||
|
|
fmt.Println("API error message:", data.Message)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// Display the weather data in the terminal window
|
||
|
|
fmt.Printf("Weather in %s\n", data.Name)
|
||
|
|
fmt.Println("===============================")
|
||
|
|
fmt.Printf("Temperature: %.1f °C\n", data.Main.Temp)
|
||
|
|
if len(data.Weather) > 0 {
|
||
|
|
fmt.Printf("Description: %s\n", data.Weather[0].Description)
|
||
|
|
}
|
||
|
|
fmt.Println("===============================")
|
||
|
|
|
||
|
|
// Wait for the enter key before exiting the program
|
||
|
|
fmt.Print("Press Enter to exit GoWeather...")
|
||
|
|
fmt.Scanln()
|
||
|
|
}
|