diff --git a/README.md b/README.md index 240d39d..7ea474c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,70 @@ func main() { } ``` +The example below shows a basic example of how the meteo package can fetch weather statuses concurrently. + +```go +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) +} +``` +Build the binary: +```bash +➜ meteo git:(master) ✗ go build -o example ./examples/concurrent/main.go +``` +Run the binary: +``` +➜ meteo git:(master) ✗ ./example +0.61s Location: Lodz,PL, Weather: Cloudy 2.3°C +0.61s Location: Killarney,IE, Weather: Cloudy 12.0°C +0.61s Location: Vilnius,LT, Weather: Cloudy 4.3°C +0.61s Location: Berlin,DE, Weather: Partlycloudy_night 3.8°C +0.62s Location: Castlebar,IE, Weather: Cloudy 12.7°C +0.62s Location: Belfast,UK, Weather: Lightrain 10.5°C +0.62s Location: Dublin,IE, Weather: Cloudy 12.2°C +0.63s Location: Warsaw,PL, Weather: Partlycloudy_night 3.6°C +0.63s Location: London,UK, Weather: Cloudy 11.5°C +2.24s Location: Vienna,AT, Weather: Cloudy 4.6°C +2.24s elapsed +``` + # Installation ``` $ go get github.com/qba73/meteo diff --git a/examples/concurrent/main.go b/examples/concurrent/main.go new file mode 100644 index 0000000..3031345 --- /dev/null +++ b/examples/concurrent/main.go @@ -0,0 +1,56 @@ +// Example of running the program below +// +// ➜ meteo git:(master) ✗ go build -o example ./examples/concurrent/main.go +// ➜ meteo git:(master) ✗ ./example +// 0.61s Location: Lodz,PL, Weather: Cloudy 2.3°C +// 0.61s Location: Killarney,IE, Weather: Cloudy 12.0°C +// 0.61s Location: Vilnius,LT, Weather: Cloudy 4.3°C +// 0.61s Location: Berlin,DE, Weather: Partlycloudy_night 3.8°C +// 0.62s Location: Castlebar,IE, Weather: Cloudy 12.7°C +// 0.62s Location: Belfast,UK, Weather: Lightrain 10.5°C +// 0.62s Location: Dublin,IE, Weather: Cloudy 12.2°C +// 0.63s Location: Warsaw,PL, Weather: Partlycloudy_night 3.6°C +// 0.63s Location: London,UK, Weather: Cloudy 11.5°C +// 2.24s Location: Vienna,AT, Weather: Cloudy 4.6°C +// 2.24s elapsed +// +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) +} diff --git a/geonames/geonames.go b/geonames/geonames.go index e7a1a77..f5403ab 100644 --- a/geonames/geonames.go +++ b/geonames/geonames.go @@ -94,7 +94,7 @@ func NewClient(userName string, opts ...Option) (*client, error) { UserName: userName, BaseURL: baseURL, HTTPClient: &http.Client{ - Timeout: time.Second * 5, + Timeout: time.Second * 10, }, } for _, opt := range opts {