-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.go
72 lines (62 loc) · 1.85 KB
/
weather.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
65
66
67
68
69
70
71
72
package api
import (
"encoding/json"
"io/ioutil"
"net/http"
)
type WeatherResponse struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Timezone string `json:"timezone"`
TimezoneOffset int `json:"timezone_offset"`
Current Current `json:"current"`
Minutely []Minute `json:"minutely"`
Alerts []Alert `json:"alerts"`
}
type Current struct {
Dt int `json:"dt"`
Sunrise int `json:"sunrise"`
Sunset int `json:"sunset"`
Temp float64 `json:"temp"`
FeelsLike float64 `json:"feels_like"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
DewPoint float64 `json:"dew_point"`
Uvi int `json:"uvi"`
Clouds int `json:"clouds"`
Visibility int `json:"visibility"`
WindSpeed float64 `json:"wind_speed"`
WindDeg int `json:"wind_deg"`
Weather []WeatherState `json:"weather"`
}
type WeatherState struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
}
type Alert struct {
SenderName string `json:"sender_name"`
Event string `json:"event"`
Start int `json:"start"`
End int `json:"end"`
Description string `json:"description"`
Tags []string `json:"tags"`
}
type Minute struct {
Dt int `json:"dt"`
Precipitation int `json:"precipitation"`
}
func FetchWeatherData(url string) *WeatherResponse {
var weatherResponse WeatherResponse
resp, err := http.Get(url)
if err != nil {
panic("http request error")
}
jsonBody, bErr := ioutil.ReadAll(resp.Body)
if bErr != nil {
panic("body read error")
}
json.Unmarshal(jsonBody, &weatherResponse)
return &weatherResponse
}