1# OpenWeatherMap Go API
2
3[![GoDoc](https://godoc.org/github.com/briandowns/openweathermap?status.svg)](https://godoc.org/github.com/briandowns/openweathermap) [![Build Status](https://travis-ci.org/briandowns/openweathermap.svg?branch=master)](https://travis-ci.org/briandowns/openweathermap) [![Coverage Status](https://coveralls.io/repos/github/briandowns/openweathermap/badge.svg?branch=master)](https://coveralls.io/github/briandowns/openweathermap?branch=master)
4
5Go (golang) package for use with openweathermap.org's API.
6
7For more detail about the library and its features, reference your local godoc once installed.
8
9[Website](https://briandowns.github.io/openweathermap)!
10
11To use the OpenweatherMap API, you need to obtain an API key.  Sign up [here](http://home.openweathermap.org/users/sign_up).  Once you have your key, create an environment variable called `OWM_API_KEY`.  Start coding!
12
13[Slack Channel](https://openweathermapgolang.slack.com/messages/general)
14
15Contributions welcome!
16
17## Features
18
19### Current Weather Conditions
20
21- By City
22- By City,St (State)
23- By City,Co (Country)
24- By City ID
25- By Zip,Co (Country)
26- By Longitude and Latitude
27
28## Forecast
29
30Get the weather conditions for a given number of days.
31
32- By City
33- By City,St (State)
34- By City,Co (Country)
35- By City ID
36- By Longitude and Latitude
37
38### Access to Condition Codes and Icons
39
40Gain access to OpenWeatherMap icons and condition codes.
41
42- Thunderstorms
43- Drizzle
44- Rain
45- Snow
46- Atmosphere
47- Clouds
48- Extreme
49- Additional
50
51### Data Available in Multiple Measurement Systems
52
53- Fahrenheit (OpenWeatherMap API - imperial)
54- Celsius (OpenWeatherMap API - metric)
55- Kelvin (OpenWeatherMap API - internal)
56
57### UV Index Data
58
59- Current
60- Historical
61
62### Pollution Data
63
64- Current
65
66## Historical Conditions
67
68- By Name
69- By ID
70- By Coordinates
71
72## Supported Languages
73
74English - en, Russian - ru, Italian - it, Spanish - es (or sp), Ukrainian - uk (or ua), German - de, Portuguese - pt, Romanian - ro, Polish - pl, Finnish - fi, Dutch - nl, French - fr, Bulgarian - bg, Swedish - sv (or se), Chinese Traditional - zh_tw, Chinese Simplified - zh (or zh_cn), Turkish - tr, Croatian - hr, Catalan - ca
75
76## Installation
77
78```bash
79go get github.com/briandowns/openweathermap
80```
81
82## Examples
83
84There are a few full examples in the examples directory that can be referenced.  1 is a command line application and 1 is a simple web application.
85
86```Go
87package main
88
89import (
90	"log"
91	"fmt"
92	"os"
93
94	// Shortening the import reference name seems to make it a bit easier
95	owm "github.com/briandowns/openweathermap"
96)
97
98var apiKey = os.Getenv("OWM_API_KEY")
99
100func main() {
101	w, err := owm.NewCurrent("F", "ru", apiKey) // fahrenheit (imperial) with Russian output
102	if err != nil {
103		log.Fatalln(err)
104	}
105
106	w.CurrentByName("Phoenix")
107	fmt.Println(w)
108}
109
110```
111
112### Current Conditions by location name
113
114```Go
115func main() {
116    w, err := owm.NewCurrent("K", "EN", apiKey) // (internal - OpenWeatherMap reference for kelvin) with English output
117    if err != nil {
118        log.Fatalln(err)
119    }
120
121    w.CurrentByName("Phoenix,AZ")
122    fmt.Println(w)
123}
124```
125
126### Forecast Conditions in imperial (fahrenheit) by coordinates
127
128```Go
129func main() {
130    w, err := owm.NewForecast("5", "F", "FI", apiKey) // valid options for first parameter are "5" and "16"
131    if err != nil {
132        log.Fatalln(err)
133    }
134
135    w.DailyByCoordinates(
136        &owm.Coordinates{
137                Longitude: -112.07,
138                Latitude: 33.45,
139        },
140        5 // five days forecast
141    )
142    fmt.Println(w)
143}
144```
145
146### Current conditions in metric (celsius) by location ID
147
148```Go
149func main() {
150    w, err := owm.NewCurrent("C", "PL", apiKey)
151    if err != nil {
152        log.Fatalln(err)
153    }
154
155    w.CurrentByID(2172797)
156    fmt.Println(w)
157}
158```
159
160### Current conditions by zip code. 2 character country code required
161
162```Go
163func main() {
164    w, err := owm.NewCurrent("F", "EN", apiKey)
165    if err != nil {
166        log.Fatalln(err)
167    }
168
169    w.CurrentByZip(19125, "US")
170    fmt.Println(w)
171}
172```
173
174### Configure http client
175
176```Go
177func main() {
178    client := &http.Client{}
179    w, err := owm.NewCurrent("F", "EN", apiKey, owm.WithHttpClient(client))
180    if err != nil {
181        log.Fatalln(err)
182    }
183}
184```
185
186### Current UV conditions
187
188```Go
189func main() {
190    uv, err := owm.NewUV(apiKey)
191    if err != nil {
192        log.Fatalln(err)
193    }
194
195    coord := &owm.Coordinates{
196        Longitude: 53.343497,
197        Latitude:  -6.288379,
198    }
199
200    if err := uv.Current(coord); err != nil {
201        log.Fatalln(err)
202    }
203
204    fmt.Println(coord)
205}
206```
207
208### Historical UV conditions
209
210```Go
211func main() {
212    uv, err := owm.NewUV(apiKey)
213    if err != nil {
214        log.Fatalln(err)
215    }
216
217    coord := &owm.Coordinates{
218        Longitude: 54.995656,
219        Latitude:  -7.326834,
220    }
221
222    end := time.Now().UTC()
223    start := time.Now().UTC().Add(-time.Hour * time.Duration(24))
224
225    if err := uv.Historical(coord, start, end); err != nil {
226        log.Fatalln(err)
227    }
228}
229```
230
231### UV Information
232
233```Go
234func main() {
235    uv, err := owm.NewUV(apiKey)
236    if err != nil {
237        log.Fatalln(err)
238    }
239
240    coord := &owm.Coordinates{
241    	Longitude: 53.343497,
242    	Latitude:  -6.288379,
243    }
244
245    if err := uv.Current(coord); err != nil {
246    	log.Fatalln(err)
247    }
248
249    info, err := uv.UVInformation()
250    if err != nil {
251        log.Fatalln(err)
252    }
253
254    fmt.Println(info)
255}
256```
257
258### Pollution Information
259
260```Go
261func main() {
262    pollution, err := owm.NewPollution(apiKey)
263    if err != nil {
264        log.Fatalln(err)
265    }
266
267    params := &owm.PollutionParameters{
268        Location: owm.Coordinates{
269            Latitude:  0.0,
270            Longitude: 10.0,
271        },
272        Datetime: "current",
273    }
274
275    if err := pollution.PollutionByParams(params); err != nil {
276        log.Fatalln(err)
277    }
278}
279```
280