Skip to content

Instantly share code, notes, and snippets.

@gudmundur
Created January 12, 2014 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gudmundur/8385399 to your computer and use it in GitHub Desktop.
Save gudmundur/8385399 to your computer and use it in GitHub Desktop.
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, &currencies)
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