Golang : Get currencies exchange rates example




Problem :

You are building an e-commerce website/application and you want to display prices in certain currencies. You need to download latest currencies exchange rates. How to do that?

NOTE : It is good idea to store the rates into local database. It helps in situation when the connection to the external source is down and also consider speed in calculating the converted price

NOTE : Remember to execute the Golang program from cronjob to update rates on daily or hourly basis.

Solution :

Use data from http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml(daily) or https://openexchangerates.org/api/latest.json (need API key to access). For this example, we will use the (daily) XML data from ECB. If you are looking for the historical data, see http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml

Here you go!

 package main

 import (
 "encoding/xml"
 "fmt"
 "io/ioutil"
 "log"
 "net/http"
 )



 type Envelope struct {
 Cube []struct {
 Date  string `xml:"time,attr"`
 Rates []struct {
 Currency string `xml:"currency,attr"`
 Rate string `xml:"rate,attr"`
 } `xml:"Cube"`
 } `xml:"Cube>Cube"`
 }

 func main() {
 // get the latest exchange rate 
 resp, err := http.Get("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")

 if err != nil {
 log.Fatal(err)
 }

 defer resp.Body.Close()

 xmlCurrenciesData, err := ioutil.ReadAll(resp.Body)

 if err != nil {
 log.Fatal(err)
 }


 var env Envelope
 err = xml.Unmarshal(xmlCurrenciesData, &env)

 if err != nil {
 log.Fatal(err)
 }

 fmt.Println("Date ", env.Cube[0].Date)

 for _, v := range env.Cube[0].Rates {
 fmt.Println("Currency : ", v.Currency, " Rate : ", v.Rate)
 }

 // REMEMBER! this data is from European Central Bank
 // therefore the rates are based on EUR

 }

Sample output :

 Date  2015-07-31
 Currency :  USD  Rate :  1.0967
 Currency :  JPY  Rate :  136.34
 Currency :  BGN  Rate :  1.9558
 Currency :  CZK  Rate :  27.031
 Currency :  DKK  Rate :  7.4615
 Currency :  GBP  Rate :  0.70410
 Currency :  HUF  Rate :  308.30
 Currency :  PLN  Rate :  4.1435
 Currency :  RON  Rate :  4.4048
 Currency :  SEK  Rate :  9.4622
 Currency :  CHF  Rate :  1.0565
 Currency :  NOK  Rate :  9.0015
 Currency :  HRK  Rate :  7.5920
 Currency :  RUB  Rate :  66.8596
 Currency :  TRY  Rate :  3.0485
 Currency :  AUD  Rate :  1.5140
 Currency :  BRL  Rate :  3.6974
 Currency :  CAD  Rate :  1.4310
 Currency :  CNY  Rate :  6.8102
 Currency :  HKD  Rate :  8.5032
 Currency :  IDR  Rate :  14866.29
 Currency :  ILS  Rate :  4.1440
 Currency :  INR  Rate :  70.3382
 Currency :  KRW  Rate :  1287.41
 Currency :  MXN  Rate :  17.7473
 Currency :  MYR  Rate :  4.2015
 Currency :  NZD  Rate :  1.6769
 Currency :  PHP  Rate :  50.146
 Currency :  SGD  Rate :  1.5082
 Currency :  THB  Rate :  38.571
 Currency :  ZAR  Rate :  13.9210

  See also : Golang : Get all countries currencies code in JSON format





By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement