Thin wrapper for Fixer.io
A thin wrapper in Go for Fixer.io, a service for foreign exchange rates and currency conversion. It provides a few methods to easily construct the url, makes the api call and gives back the response.
Installation
As for any other package, you can use go get
:
$ go get github.com/unkeep/gofixerio
For better package management however, I'd recommend glide or the still in alpha dep.
Usage
First, let's import the package:
import "github.com/unkeep/gofixerio"
Let's see an exhaustive example with all the parameters:
exchange := fixerio.New()
exchange.Base(fixerio.EUR)
exchange.ApiKey("YOUR_PERSONAL_API_KEY")
exchange.Symbols(fixerio.USD, fixerio.AUD)
exchange.Secure(true)
exchange.Historical(time.Date(2016, time.December, 15, 0, 0, 0, 0, time.UTC))
if rates, err := exchange.GetRates(); err == nil {
fmt.Println(rates[fixerio.USD])
}
Response
The response is a simple map[string]float32
with currencies as keys and ratios as values. For a request like the following:
exchange := fixerio.New()
exchange.Symbols(fixerio.USD, fixerio.GBP)
rates, _ := exchange.GetRates()
fmt.Println(rates)
the response will be the map:
map[USD:1.1188 GBP:0.87093]
which you can access with the keys as strings or using the currency constants:
rates["USD"];
rates[fixerio.GBP];