Created
January 12, 2014 14:43
-
-
Save gudmundur/8385399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "io/ioutil" | |
import "net/http" | |
import "encoding/json" | |
type CurrencyResult struct { | |
Currencies []Currency `json:"results"` | |
} | |
type Currency struct { | |
ShortName string `json:"shortName"` | |
LongName string `json:"longName"` | |
Value float32 `json:"value"` | |
AskValue float32 `json:"askValue"` | |
BidValue float32 `json:"bidValue"` | |
} | |
func GetCurrencies() (currencies CurrencyResult, err error) { | |
resp, err := http.Get("http://apis.is/currency/lb") | |
if err != nil { | |
fmt.Println("Error downloading currencies: %s", err) | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("Error in reading body: %s", err) | |
return | |
} | |
err = json.Unmarshal(body, ¤cies) | |
if err != nil { | |
fmt.Println("Error in unmarshaling JSON: %s", err) | |
return | |
} | |
return | |
} | |
func main() { | |
currencies, err := GetCurrencies() | |
if err != nil { | |
fmt.Println("Argh") | |
return | |
} | |
for _, c := range currencies.Currencies { | |
fmt.Println(c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment