Giter Site home page Giter Site logo

openweathermap's Introduction

OpenWeatherMap Go Wrapper

Package openweathermap implements a client for the OpenWeatherMap API.

Note: At this time, this package supports the Current weather data endpoint only.

Before using the OpenWeatherMap API, you need to register here and read the API Guidelines.

Installation

go get github.com/ayoisaiah/openweathermap

Then import the package into your project:

import (
    github.com/ayoisaiah/openweathermap
)

Creating an instance

An API Key from OpenWeatherMap is required

owm, err := openweathermap.New("YOUR_API_KEY")
if err != nil {
  log.Fatalln(err)
}

// do something with `owm`

You may optionally provide a custom HTTP client:

owm, err := openweathermap.New("{YOUR_API_KEY}", WithHTTPClient(http.DefaultClient))
if err != nil {
  log.Fatalln(err)
}

// do something with `owm`

Or set your preferred temperature unit (one of F (Fahrenheit), C (Celsuis), K(Kelvin)). The default is Kelvin.

owm, err := openweathermap.New("{YOUR_API_KEY}", WithUnit("C"))
if err != nil {
  log.Fatalln(err)
}

// do something with `owm`

Or change the default language (English), to any of the supported languages below:

owm, err := openweathermap.New("{YOUR_API_KEY}", WithLang("fr"))
if err != nil {
  log.Fatalln(err)
}

// do something with `owm`

Get the current weather forecast

By city name

forecast, err := owm.GetCurrentByCityName("Lagos")
if err != nil {
  fmt.Println(err)
  return
}

fmt.Printf("%+v", forecast)

By coordinates

forecast, err := owm.GetCurrentByCoords(&openweathermap.Coord{
  Lat: 8.5,
  Lon: 4.5,
})
if err != nil {
  fmt.Println(err)
  return
}

fmt.Printf("%+v", forecast)

By city ID

forecast, err := owm.GetCurrentByID(4513583)
if err != nil {
  fmt.Println(err)
  return
}

fmt.Printf("%+v", forecast)

By Zip code

forecast, err := owm.GetCurrentByZipCode(94040, "us")
if err != nil {
  fmt.Println(err)
  return
}

fmt.Printf("%+v", forecast)

Supported languages

English - 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

Testing

Ensure the OPENWEATHERMAP_KEY environmental variable is set before running the tests

env OPENWEATHERMAP_KEY="YOUR_API_KEY" go test

Licence

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.