Skip to content

theovidal/goweather

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

goweather

🌦 Easily fetch weather information using Go and the OpenWeatherMap API.

codecov badge CircleCI Badge Travis Badge License Badge Codacy Badge Go Report Badge Godoc Badge Latest version Badge Code size Badge Open issues Badge

πŸ”§ Installation

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 !

⌨ Usage

Initializing the API

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)

Get current weather

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

Get forecast

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

More examples

Check the examples file for more examples of scripts using the goweather library.

πŸ’» Developing

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 a WEATHER_API_KEY environment variable
  • Commit and push your work
  • Open a pull request and explain why you made these changes

πŸ“œ Credits

πŸ” License

MIT License