-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (53 loc) · 1.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/joho/godotenv"
)
// WeatherAPI JSON Structure
type Weather struct {
Location struct {
Name string `json:"name"`
Country string `json:"country"`
} `json:"location"`
Current struct {
TempC float64 `json:"temp_c"`
Condition struct {
Text string `json:"text"`
} `json:"condition"`
} `json:"current"`
Forecast struct {
Forecastday []struct {
Hour []struct {
TimeEpoch int64 `json:"time_epoch`
} `json:"hour"`
} `json:"forecastday"`
} `json:"forecast"`
}
func main() {
// Getting city name as first argument from the command-line
cityName := os.Args[0]
// Getting the API key from env file
err := godotenv.Load(".env")
if err != nil {
panic("Error loading .env file")
}
API_KEY := os.Getenv("API_KEY")
API_PATH := "http://api.weatherapi.com/v1/forecast.json?key=" + API_KEY + "&q=" + cityName + "&days=1&aqi=no&alerts=no"
res, err := http.Get(API_PATH)
if err != nil {
panic(err)
}
// Returned when func main returns
defer res.Body.Close()
if res.StatusCode != 200 {
panic("Weather API not available")
}
body, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}