π¦ Easily fetch weather information using Go and the OpenWeatherMap API.
First of all, make sure you have Go 1.12 or earlier installed on your system.
Install the library using go get
...
go get github.com/theovidal/goweather
... or use whatever dependency manager you like! Then, you can start writing your code !
Before continuing, head over to openweathermap.org and grab an API key. It's free up to 60 requests per minute, which is pretty comfortable.
After this step, you're ready to initialize the API using the goweather.NewAPI
function :
package main
import "github.com/theovidal/goweather"
func main() {
api, err := goweather.NewAPI("ez5e4fz44e84fg89ze8", "en", "metric")
if err != nil {
panic(err)
}
}
This function takes three parameters :
- the API token that you generated using your account on openweathermap.org
- the language to use. It can be one of these Arabic -
ar
, Bulgarian -bg
, Catalan -ca
, Czech -cz
, German -de
, Greek -el
, English -en
, Persian (Farsi) -fa
, Finnish -fi
, French -fr
, Galician -gl
, Croatian -hr
, Hungarian -hu
, Italian -it
, Japanese -ja
, Korean -kr
, Latvian -la
, Lithuanian -lt
, Macedonian -mk
, Dutch -nl
, Polish -pl
, Portuguese -pt
, Romanian -ro
, Russian -ru
, Swedish -se
, Slovak -sk
, Slovenian -sl
, Spanish -es
, Turkish -tr
, Ukrainian -ua
, Vietnamese -vi
, Chinese Simplified -zh_cn
, Chinese Traditional -zh_tw
- the unit system. It can be one of these :
default
(temperatures in Kelvin)metric
(temperatures in Celsius)imperial
(temperatures in Fahrenheit)
To get the current weather, use the Current
method of the API :
weather, err := api.Current("Lyon,FR")
if err != nil {
panic(err)
}
You can use any existing location in the parameter, but it's recommended to use the City,Country code
format so the result is 100% accurate.
The method returns a types.Current
structure that you can explore on GoDoc
To get the forecast, use the Forecast
method of the API :
weather, err := api.Forecast("Lyon,FR")
if err != nil {
panic(err)
}
You can use any existing location in the parameter, but it's recommended to use the City,Country code
format so the result is 100% accurate.
The method returns a types.Forecast
structure that you can explore on GoDoc
Check the examples file for more examples of scripts using the goweather library.
Thank you for being interested in goweather's development ! Please follow these steps :
- Fork the repository on your profile
- Clone it on your computer and install the required dependencies using the
go get
command - Make your changes
- Write tests and execute them using the
go test
command. Your API key must be stored in aWEATHER_API_KEY
environment variable - Commit and push your work
- Open a pull request and explain why you made these changes
- Service : OpenWeatherMap
- Maintainer : ThΓ©o Vidal