qba73 / meteo

Go client library for the Norwegian Meteorological Institute and the Norwegian Broadcasting Corporation weather API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Reference Go Go Report Card GitHub GitHub go.mod Go version

meteo

Meteo is a Go client library for the weather and meteorological forecast from Yr.

Weather forecast from Yr, delivered by the Norwegian Meteorological Institute and NRK.

Usage

You must register your user agent string in the YR.NO service and your user name at the GeoNames.org.

package main

import (
	"fmt"

	"github.com/qba73/meteo"
)

func main() {
	// Export GEO_USERNAME Env Var (you registered at geonames.org)

	// Get current weather for given location.
	weather, err := meteo.GetWeather("Vilnius,LT")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(weather)
	// Cloudy 4.2°C

	fmt.Println(weather.Summary)
	// cloudy

	fmt.Println(weather.Temp)
	// 4.2
}

The code sample below shows a basic example of how the meteo package can fetch weather statuses concurrently.

package main

import (
	"fmt"
	"time"

	"github.com/qba73/meteo"
)

func main() {
	start := time.Now()
	ch := make(chan string)

	locations := []string{
		"Vilnius,LT", "Dublin,IE", "London,UK", "Berlin,DE",
		"Belfast,UK", "Castlebar,IE", "Killarney,IE",
		"Warsaw,PL", "Lodz,PL", "Vienna,AT"}

	for _, loc := range locations {
		go getWeather(loc, ch)
	}

	for range locations {
		fmt.Println(<-ch)
	}

	fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}

func getWeather(location string, ch chan<- string) {
	start := time.Now()

	weather, err := meteo.GetWeather(location)
	if err != nil {
		ch <- fmt.Sprint(err)
		return
	}
	sec := time.Since(start).Seconds()
	ch <- fmt.Sprintf("%.2fs Location: %s, Weather: %s", sec, location, weather)
}

Installation

$ go install github.com/qba73/meteo/cmd/meteo@latest

About

Go client library for the Norwegian Meteorological Institute and the Norwegian Broadcasting Corporation weather API.

License:MIT License


Languages

Language:Go 85.9%Language:Makefile 14.1%