From 663df0e2908b0b328ed35591338e18fae482f955 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 21 Sep 2015 17:47:00 +0200 Subject: Fix some float/int bugs and error reporting --- example/main.go | 10 ++++++---- weather.go | 29 +++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/example/main.go b/example/main.go index 3eddcc8..e8c03c7 100644 --- a/example/main.go +++ b/example/main.go @@ -8,9 +8,11 @@ import ( ) func main() { - current, err := weather.ByCityName("Berlin") - if err != nil { - log.Fatal(err) + for _, loc := range []string{"Berlin", "London", ""} { + current, err := weather.ByCityName(loc) + if err != nil { + log.Fatal(err) + } + fmt.Println(current) } - fmt.Println(current) } diff --git a/weather.go b/weather.go index ab1f472..a55347c 100644 --- a/weather.go +++ b/weather.go @@ -27,12 +27,20 @@ type Current struct { Sys Sys ID int // City ID Name string // City Name - Cod int // Internal Parameter + Cod int `json:"-"` // bogus int/string + Message string } func (c Current) String() string { - return fmt.Sprintf("%v: %v, %v, %v", - c.Name, c.Weather[0], c.Main, c.Wind) + if c.Message != "" { + return c.Message + } + s := fmt.Sprintf("%v: ", c.Name) + for _, w := range c.Weather { + s += fmt.Sprintf("%v, ", w) + } + s += fmt.Sprintf("%v, %v", c.Main, c.Wind) + return s } type Coord struct { @@ -47,14 +55,14 @@ func (c Coord) String() string { type Cond int type Weather struct { - Cond Cond `json:"id"` // Station ID + ID Cond `json:"id"` // Station ID Main string // Group of weather parameters Description string // Weather condition within the group Icon string // Weather con id } func (w Weather) String() string { - return fmt.Sprint(w.Cond) + return fmt.Sprint(w.ID) } type Main struct { @@ -75,10 +83,15 @@ func (m Main) String() string { type Wind struct { Speed float64 // Wind speed. Default meter/sec Deg int // Wind direction, degrees (meteorogical) + Gust float64 } func (w Wind) String() string { - return fmt.Sprintf("wind: %v° %v m/s", w.Deg, w.Speed) + s := fmt.Sprintf("wind: %v° %v m/s", w.Deg, w.Speed) + if w.Gust != 0 { + s += fmt.Sprintf(" gust %v m/s", w.Gust) + } + return s } type Clouds struct { @@ -90,7 +103,7 @@ func (c Clouds) String() string { } type Rain struct { - Vol int `json:"3h"` // Rain volume for last 3 hours + Vol float64 `json:"3h"` // Rain volume for last 3 hours } func (r Rain) String() string { @@ -101,7 +114,7 @@ func (r Rain) String() string { } type Snow struct { - Vol int `json:"3h"` // Snow volume for last 3 hours + Vol float64 `json:"3h"` // Snow volume for last 3 hours } func (s Snow) String() string { -- cgit v1.2.3