/
quakes.go
42 lines (35 loc) · 976 Bytes
/
quakes.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
package main
import (
"fmt"
"time"
"os"
"io/ioutil"
"net/http"
"encoding/json"
)
func main() {
dateFmt := "2006-01-02"
url := "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson"
fullUrl := url +
"&starttime=" + time.Now().Add(-24 * time.Hour).Format(dateFmt) +
"&endtime=" + time.Now().Format(dateFmt)
resp, e := http.Get(fullUrl)
if e != nil {
fmt.Println("Error requesting data: " + e.Error())
os.Exit(1)
}
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
var data interface{}
json.Unmarshal(body, &data)
for _, feature := range arr(obj(data)["features"]) {
props := obj(obj(feature)["properties"])
fmt.Println(props["place"], props["mag"])
}
}
func obj(val interface{}) (map[string]interface{}) {
return val.(map[string]interface{})
}
func arr(val interface{}) ([]interface{}) {
return val.([]interface{})
}