goweather

package module
v0.1.2-0...-5405eed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2021 License: MIT Imports: 8 Imported by: 0

README

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

Documentation

Overview

Package goweather lets anybody easily fetch weather information using the OpenWeatherMap API

Before playing around with the library, get an API key at https://openweathermap.org.

Create an API structure :

api, _ := goweather.NewAPI("token", "en", "metric")

Get the current weather using this API :

data, _ := api.Current("Lyon, FR")

Get the forecast using this API :

data, _ := api.Forecast("Lyon, FR")

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	// contains filtered or unexported fields
}

API is the main piece of the library : it centralizes requests made to OpenWeatherMap and parameters.

func NewAPI

func NewAPI(key string, lang string, unit string) (api API, err error)

NewAPI creates a new weather API structure

func (API) Current

func (api API) Current(location string) (current types.Current, err error)

Current gets current weather information for a specific location

Example

Example of fetching current weather

api, err := NewAPI("", "en", "metric")
if err != nil {
	panic(err)
}
data, err := api.Current("Lyon,FR")
if err != nil {
	panic(err)
}

fmt.Println(data.City.Name, ":", data.Conditions.Description)
if data.Conditions.Clouds < 20 {
	println("What a beautiful moment ! Go outside !")
}
Output:

func (API) Forecast

func (api API) Forecast(location string) (forecast types.Forecast, err error)

Forecast get forecasts for a specific location

Example

Example of fetching forecast

api, err := NewAPI("", "en", "metric")
if err != nil {
	panic(err)
}
data, err := api.Forecast("Lyon,FR")
if err != nil {
	panic(err)
}

fmt.Println(data.City.Name)
println("---------------")

for _, condition := range data.Conditions {
	conditionTime := time.Unix(int64(condition.Timestamp), 0)
	fmt.Printf("At %d:%d : %s", conditionTime.Hour(), conditionTime.Minute(), condition.Description)
}
Output:

Directories

Path Synopsis
Package types contains all the types associated to the goweather library : weather conditions, cities...
Package types contains all the types associated to the goweather library : weather conditions, cities...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL