|
1 | 1 | # OpenWeatherMap w/ golang |
2 | | -A lightweight wrapper for the OpenWeatherMap API for use with golang |
| 2 | +A lightweight wrapper for the OpenWeatherMap API for use with golang |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +First, create an instance of an OpenWeatherMap struct with your APP ID |
| 7 | +```go |
| 8 | +package main |
| 9 | +import "github.com/ramsgoli/openweathermap" |
| 10 | + |
| 11 | +owm := openweathermap.OpenWeatherMap{API_KEY: os.Getenv("OWM_APP_ID")} |
| 12 | +``` |
| 13 | + |
| 14 | +### Fetching the current weather |
| 15 | + |
| 16 | +Create an instance of the CurrentWeatherMap struct. |
| 17 | + |
| 18 | +If fetching the weather from a specific city, pass the city name as an argument to the CurrentWeatherFromCity function |
| 19 | +```go |
| 20 | +var currentWeather *openweathermap.CurrentWeatherResponse |
| 21 | +var err error |
| 22 | + |
| 23 | +currentWeather, err = owm.CurrentWeatherFromCity(city) |
| 24 | +``` |
| 25 | + |
| 26 | +If fetching the weather from geocoordinates, pass the latitude and longitudeas arguments to the CurrentWeatherFromCoordinates function |
| 27 | +```go |
| 28 | +var currentWeather *openweathermap.CurrentWeatherResponse |
| 29 | +var err error |
| 30 | + |
| 31 | +currentWeather, err = owm.CurrentWeatherFromCoordinates(lat, long) |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +This function returns a struct, (CurrentWeatherResonse) that matches the fields of the json response from the API |
| 36 | +```json |
| 37 | +{"coord":{"lon":139,"lat":35}, |
| 38 | +"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049}, |
| 39 | +"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}], |
| 40 | +"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04}, |
| 41 | +"wind":{"speed":7.31,"deg":187.002}, |
| 42 | +"rain":{"3h":0}, |
| 43 | +"clouds":{"all":92}, |
| 44 | +"dt":1369824698, |
| 45 | +"id":1851632, |
| 46 | +"name":"Shuzenji", |
| 47 | +"cod":200} |
| 48 | +``` |
| 49 | + |
| 50 | +To access fields of the struct, use the json object's field name with the first letter capitalized |
| 51 | +```go |
| 52 | +fmt.Printf("The current temperature in %s is %.2f degrees\n", currentWeather.Name, currentWeather.Main.Temp) |
| 53 | +``` |
| 54 | + |
0 commit comments