-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetWeatherFromZip.go
55 lines (48 loc) · 1.27 KB
/
GetWeatherFromZip.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
// Get API key at www.interzoid.com/register
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Payload struct {
City string
State string
TempF string
TempC string
Weather string
WindMPH string
WindDir string
RelativeHumidity string
VisibilityMiles string
AirQualityIndex string
AirQualityCode string
AirQuality string
Sunrise string
Sunset string
DaylightHours string
DaylightMinutes string
Code string
Credits int
}
func main() {
// Create the struct variable to store the decoded JSON response
thePayload := Payload{}
// Call the API
// Can error check here too replacing underscore with 'err'
response, _ := http.Get("https://api.interzoid.com/getweatherzipcode?license=
YOURAPILICENSEKEY&zip=94111")
if response.StatusCode != 200 {
// Report any HTTP Errors
fmt.Println("Error: ", response.Status)
} else {
// Decode the JSON and print a comma-delimited response,
// one of many ways to format the response
_ = json.NewDecoder(response.Body).Decode(&thePayload)
// display some of the fields
fmt.Println(thePayload.City + "," + thePayload.State +
"," + thePayload.TempF +
"," + thePayload.TempC +
"," + thePayload.Weather)
}
}