Skip to content

Instantly share code, notes, and snippets.

@epoch
Created March 3, 2015 08:23
Show Gist options
  • Select an option

  • Save epoch/e8a7fee37bbd1d626e5a to your computer and use it in GitHub Desktop.

Select an option

Save epoch/e8a7fee37bbd1d626e5a to your computer and use it in GitHub Desktop.
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