Golang : Get all countries currencies code in JSON format




For this tutorial, we will learn how to get all the currencies names from http://country.io/currency.json and un-marshall them into a map. The next challenge will be getting their values against the US dollar, but we will put that into another tutorial.

Here you go!

 package main

 import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"
  "os"
 )

 func main() {

  resp, err := http.Get("http://country.io/currency.json")

  if err != nil {
 fmt.Println(err)
 os.Exit(1)
  }

  defer resp.Body.Close()

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

  if err != nil {
 fmt.Println(err)
 os.Exit(1)
  }

  //fmt.Println(string(jsonData))

  currenciesMap := make(map[string]interface{})

  // Decode JSON into our map
  err = json.Unmarshal([]byte(jsonCurrenciesData), &currenciesMap)

  if err != nil {
 println(err)
 return
  }

  for iso2, moneyName := range currenciesMap {
 fmt.Println("ISO2 code:", iso2, "Currency name :", moneyName)
  }

 }

Output :

 ISO2 code: BL Currency name : EUR
 ISO2 code: GE Currency name : GEL
 ISO2 code: SO Currency name : SOS
 ISO2 code: SH Currency name : SHP
 ISO2 code: SC Currency name : SCR
 ISO2 code: UM Currency name : USD
 ISO2 code: TG Currency name : XOF
 ISO2 code: GM Currency name : GMD
 ISO2 code: MF Currency name : EUR
 ISO2 code: MQ Currency name : EUR
 ISO2 code: MX Currency name : MXN
 ISO2 code: VU Currency name : VUV
 ISO2 code: LC Currency name : XCD
 ISO2 code: LV Currency name : EUR
 ISO2 code: AO Currency name : AOA
 ISO2 code: TC Currency name : USD
 ISO2 code: BS Currency name : BSD
 ISO2 code: OM Currency name : OMR
 ISO2 code: PE Currency name : PEN
 ISO2 code: MG Currency name : MGA
 ISO2 code: MU Currency name : MUR
 ISO2 code: NE Currency name : XOF
 ISO2 code: KP Currency name : KPW
 ISO2 code: SN Currency name : XOF
 ISO2 code: IM Currency name : GBP
 ISO2 code: AL Currency name : ALL
 ISO2 code: NA Currency name : NAD
 ISO2 code: NU Currency name : NZD
 ISO2 code: DO Currency name : DOP
 ISO2 code: AE Currency name : AED
 ISO2 code: TK Currency name : NZD
 ISO2 code: KG Currency name : KGS
 ISO2 code: LA Currency name : LAK
 ISO2 code: AQ Currency name : <----- empty??
 ISO2 code: WF Currency name : XPF
 ISO2 code: GF Currency name : EUR
 ISO2 code: KY Currency name : KYD
 ISO2 code: SG Currency name : SGD
 ISO2 code: DK Currency name : DKK
 ISO2 code: AR Currency name : ARS
 ISO2 code: BA Currency name : BAM
 ISO2 code: BJ Currency name : XOF
 ISO2 code: PL Currency name : PLN
 ISO2 code: GN Currency name : GNF
 ISO2 code: ZA Currency name : ZAR
 ISO2 code: MV Currency name : MVR
 ISO2 code: CA Currency name : CAD
 ISO2 code: CF Currency name : XAF
 ISO2 code: BR Currency name : BRL
 ISO2 code: ZM Currency name : ZMK
 ISO2 code: MM Currency name : MMK
 ISO2 code: BN Currency name : BND
 ISO2 code: BY Currency name : BYR
 ISO2 code: RU Currency name : RUB
 ISO2 code: GS Currency name : GBP
 ISO2 code: MP Currency name : USD
 ISO2 code: HU Currency name : HUF
 ISO2 code: PF Currency name : XPF
 ISO2 code: FO Currency name : DKK
 ISO2 code: TD Currency name : XAF
 ISO2 code: IN Currency name : INR
 ISO2 code: GP Currency name : EUR
 ISO2 code: ST Currency name : STD
 ISO2 code: VI Currency name : USD
 ISO2 code: MT Currency name : EUR
 ISO2 code: MR Currency name : MRO
 ISO2 code: VC Currency name : XCD
 ISO2 code: PR Currency name : USD
 ISO2 code: PG Currency name : PGK
 ISO2 code: PK Currency name : PKR
 ISO2 code: CG Currency name : XAF
 ISO2 code: KZ Currency name : KZT
 ISO2 code: BV Currency name : NOK
 ISO2 code: DJ Currency name : DJF
 ISO2 code: MZ Currency name : MZN
 ISO2 code: SI Currency name : EUR
 ISO2 code: GH Currency name : GHS
 ISO2 code: EH Currency name : MAD
 ISO2 code: ME Currency name : EUR
 ISO2 code: MH Currency name : USD
 ISO2 code: MK Currency name : MKD
 ISO2 code: MY Currency name : MYR
 ISO2 code: NL Currency name : EUR
 ISO2 code: AS Currency name : USD
 ISO2 code: RE Currency name : EUR
 ISO2 code: SX Currency name : ANG
 ISO2 code: SR Currency name : SRD
 ISO2 code: MA Currency name : MAD
 ISO2 code: FI Currency name : EUR
 ISO2 code: CN Currency name : CNY
 ISO2 code: SY Currency name : SYP
 ISO2 code: EE Currency name : EUR
 ISO2 code: MN Currency name : MNT
 ISO2 code: AT Currency name : EUR
 ISO2 code: GQ Currency name : XAF
 ISO2 code: HM Currency name : AUD
 ISO2 code: CI Currency name : XOF
 ISO2 code: CW Currency name : ANG
 ISO2 code: DZ Currency name : DZD
 ISO2 code: EC Currency name : USD
 ISO2 code: CY Currency name : EUR
 ISO2 code: TV Currency name : AUD
 ISO2 code: BW Currency name : BWP
 ISO2 code: GA Currency name : XAF
 ISO2 code: IQ Currency name : IQD
 ISO2 code: BB Currency name : BBD
 ISO2 code: TN Currency name : TND
 ISO2 code: NF Currency name : AUD
 ISO2 code: KE Currency name : KES
 ISO2 code: DM Currency name : XCD
 ISO2 code: JO Currency name : JOD
 ISO2 code: CL Currency name : CLP
 ISO2 code: SK Currency name : EUR
 ISO2 code: AF Currency name : AFN
 ISO2 code: IO Currency name : USD
 ISO2 code: FK Currency name : FKP
 ISO2 code: LR Currency name : LRD
 ISO2 code: TH Currency name : THB
 ISO2 code: GY Currency name : GYD
 ISO2 code: EG Currency name : EGP
 ISO2 code: MS Currency name : XCD
 ISO2 code: NC Currency name : XPF
 ISO2 code: NZ Currency name : NZD
 ISO2 code: CM Currency name : XAF
 ISO2 code: CX Currency name : AUD
 ISO2 code: AI Currency name : XCD
 ISO2 code: AW Currency name : AWG
 ISO2 code: QA Currency name : QAR
 ISO2 code: FJ Currency name : FJD
 ISO2 code: NR Currency name : AUD
 ISO2 code: YT Currency name : EUR
 ISO2 code: LT Currency name : LTL
 ISO2 code: SA Currency name : SAR
 ISO2 code: UG Currency name : UGX
 ISO2 code: CC Currency name : AUD
 ISO2 code: SZ Currency name : SZL
 ISO2 code: KH Currency name : KHR
 ISO2 code: SB Currency name : SBD
 ISO2 code: NO Currency name : NOK
 ISO2 code: BE Currency name : EUR
 ISO2 code: MC Currency name : EUR
 ISO2 code: FR Currency name : EUR
 ISO2 code: LK Currency name : LKR
 ISO2 code: GD Currency name : XCD
 ISO2 code: UY Currency name : UYU
 ISO2 code: BH Currency name : BHD
 ISO2 code: BQ Currency name : USD
 ISO2 code: TT Currency name : TTD
 ISO2 code: WS Currency name : WST
 ISO2 code: GW Currency name : XOF
 ISO2 code: HK Currency name : HKD
 ISO2 code: NG Currency name : NGN
 ISO2 code: BI Currency name : BIF
 ISO2 code: CD Currency name : CDF
 ISO2 code: KI Currency name : AUD
 ISO2 code: LB Currency name : LBP
 ISO2 code: TO Currency name : TOP
 ISO2 code: AG Currency name : XCD
 ISO2 code: IS Currency name : ISK
 ISO2 code: PS Currency name : ILS
 ISO2 code: PA Currency name : PAB
 ISO2 code: CK Currency name : NZD
 ISO2 code: CH Currency name : CHF
 ISO2 code: VG Currency name : USD
 ISO2 code: TZ Currency name : TZS
 ISO2 code: YE Currency name : YER
 ISO2 code: LS Currency name : LSL
 ISO2 code: BF Currency name : XOF
 ISO2 code: BM Currency name : BMD
 ISO2 code: RO Currency name : RON
 ISO2 code: ER Currency name : ERN
 ISO2 code: LI Currency name : CHF
 ISO2 code: VE Currency name : VEF
 ISO2 code: IL Currency name : ILS
 ISO2 code: SS Currency name : SSP
 ISO2 code: SE Currency name : SEK
 ISO2 code: JP Currency name : JPY
 ISO2 code: HN Currency name : HNL
 ISO2 code: PY Currency name : PYG
 ISO2 code: IT Currency name : EUR
 ISO2 code: DE Currency name : EUR
 ISO2 code: ID Currency name : IDR
 ISO2 code: GR Currency name : EUR
 ISO2 code: GG Currency name : GBP
 ISO2 code: PN Currency name : NZD
 ISO2 code: SM Currency name : EUR
 ISO2 code: AM Currency name : AMD
 ISO2 code: TR Currency name : TRY
 ISO2 code: BZ Currency name : BZD
 ISO2 code: TJ Currency name : TJS
 ISO2 code: MW Currency name : MWK
 ISO2 code: FM Currency name : USD
 ISO2 code: KM Currency name : KMF
 ISO2 code: SL Currency name : SLL
 ISO2 code: SD Currency name : SDG
 ISO2 code: HR Currency name : HRK
 ISO2 code: PH Currency name : PHP
 ISO2 code: BD Currency name : BDT
 ISO2 code: VN Currency name : VND
 ISO2 code: NI Currency name : NIO
 ISO2 code: CO Currency name : COP
 ISO2 code: CV Currency name : CVE
 ISO2 code: LU Currency name : EUR
 ISO2 code: BG Currency name : BGN
 ISO2 code: BT Currency name : BTN
 ISO2 code: RW Currency name : RWF
 ISO2 code: GT Currency name : GTQ
 ISO2 code: CU Currency name : CUP
 ISO2 code: JE Currency name : GBP
 ISO2 code: GB Currency name : GBP
 ISO2 code: KN Currency name : XCD
 ISO2 code: IR Currency name : IRR
 ISO2 code: UA Currency name : UAH
 ISO2 code: RS Currency name : RSD
 ISO2 code: GI Currency name : GIP
 ISO2 code: PM Currency name : EUR
 ISO2 code: MD Currency name : MDL
 ISO2 code: TW Currency name : TWD
 ISO2 code: IE Currency name : EUR
 ISO2 code: XK Currency name : EUR
 ISO2 code: ET Currency name : ETB
 ISO2 code: UZ Currency name : UZS
 ISO2 code: CZ Currency name : CZK
 ISO2 code: KW Currency name : KWD
 ISO2 code: AX Currency name : EUR
 ISO2 code: AZ Currency name : AZN
 ISO2 code: SJ Currency name : NOK
 ISO2 code: AD Currency name : EUR
 ISO2 code: JM Currency name : JMD
 ISO2 code: PT Currency name : EUR
 ISO2 code: US Currency name : USD
 ISO2 code: TM Currency name : TMT
 ISO2 code: HT Currency name : HTG
 ISO2 code: LY Currency name : LYD
 ISO2 code: GL Currency name : DKK
 ISO2 code: PW Currency name : USD
 ISO2 code: ML Currency name : XOF
 ISO2 code: MO Currency name : MOP
 ISO2 code: TF Currency name : EUR
 ISO2 code: BO Currency name : BOB
 ISO2 code: GU Currency name : USD
 ISO2 code: ZW Currency name : ZWL
 ISO2 code: CR Currency name : CRC
 ISO2 code: KR Currency name : KRW
 ISO2 code: AU Currency name : AUD
 ISO2 code: TL Currency name : USD
 ISO2 code: SV Currency name : USD
 ISO2 code: ES Currency name : EUR
 ISO2 code: NP Currency name : NPR
 ISO2 code: VA Currency name : EUR

References :

https://www.socketloop.com/tutorials/golang-display-list-of-countries-and-iso-codes

  See also : Golang : Display list of countries and ISO codes





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