Created
March 3, 2015 08:23
-
-
Save epoch/e8a7fee37bbd1d626e5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "net/http" | |
| "io/ioutil" | |
| ) | |
| func main() { | |
| // fmt.Printf("Hello, world!\n") | |
| body, _ := getWeatherResponseBody() | |
| fmt.Printf("Response: %s", body) | |
| } | |
| func getWeatherResponseBody() ([]byte, error) { | |
| // http://api.openweathermap.org/data/2.5/weather?q=Melbourne,au | |
| resp, err := http.Get("http://api.openweathermap.org/data/2.5/weather?q=Melbourne,au") | |
| if err != nil { | |
| fmt.Printf("Error getting weather: %v", err) | |
| return []byte(""), err | |
| } | |
| body, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| fmt.Printf("Error reading weather: %v", err) | |
| return []byte(""), err | |
| } | |
| defer resp.Body.Close() | |
| return body, nil | |
| } | |
| type City struct { | |
| Weather Weather `json:"main"` | |
| Name string `json:"name"` | |
| } | |
| type Weather struct { | |
| CurrentTemp float64 `json:"temp"` | |
| MaxTemp float64 `json:"temp_max"` | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment