Provides access to foreign exchange rates and currency conversions. exchangerate.host
go get github.com/g3kk0/go-forex
Import package and create client.
import forex "github.com/g3kk0/go-forex"
fc := forex.NewClient()
rates, err := fc.Latest()
if err != nil {
log.Println(err)
}
fmt.Printf("rates = % v", rates)
// {Base:EUR Date:2018-10-29 Rates:map[ILS:4.217 RON:4.6647 NZD:1.7401 ...]}
params := map[string]string{"base": "usd"}
rates, err := fc.Latest(params)
if err != nil {
log.Println(err)
}
fmt.Printf("rates = % v", rates)
// {Base:USD Date:2018-10-29 Rates:map[HUF:285.0364642826 ILS:3.7052983042 ...]}
params := map[string]string{"base": "usd", "symbols": "gbp,eur"}
rates, err := fc.Latest(params)
if err != nil {
log.Println(err)
}
fmt.Printf("rates = % v", rates)
// {Base:USD Date:2018-10-29 Rates:map[GBP:0.7801423425 EUR:0.8786574115]}
// currency conversion
params := map[string]string{"from": "eur", "to": "gbp", "amount": "25.62"}
conversion, err := fc.Convert(params)
if err != nil {
log.Println(err)
}
fmt.Printf("conversion = % v", conversion)
// {From:EUR To:GBP Amount:25.62 Result:22.7474856}