Golang : Get all countries phone codes



Tags : golang json-unmarshal-to-map countries-phone-codes iso

Continue from previous tutorial where we learn how to get all the currencies names, but this time it will be the countries phone codes and un-marshall them into a map. As a challenge for you - see how can you cross reference the ISO2 codes, get the countries names and add the countries names into the list.

Here you go!

 package main

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

 func main() {

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

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

  defer resp.Body.Close()

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

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

  //fmt.Println(string(jsonData))

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

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

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

  for iso2, phoneCode := range phoneMap {
 fmt.Println("ISO2 code:", iso2, "Phone code ", phoneCode)
  }

 }

Output :

 ISO2 code: RE Phone code  262
 ISO2 code: GP Phone code  590
 ISO2 code: PF Phone code  689
 ISO2 code: EG Phone code  20
 ISO2 code: SC Phone code  248
 ISO2 code: IS Phone code  354
 ISO2 code: HR Phone code  385
 ISO2 code: MR Phone code  222
 ISO2 code: SH Phone code  290
 ISO2 code: CM Phone code  237
 ISO2 code: TF Phone code
 ISO2 code: BN Phone code  673
 ISO2 code: GE Phone code  995
 ISO2 code: GM Phone code  220
 ISO2 code: NE Phone code  227
 ISO2 code: CN Phone code  86
 ISO2 code: KG Phone code  996
 ISO2 code: BI Phone code  257
 ISO2 code: GH Phone code  233
 ISO2 code: PS Phone code  970
 ISO2 code: MM Phone code  95
 ISO2 code: LB Phone code  961
 ISO2 code: AD Phone code  376
 ISO2 code: AF Phone code  93
 ISO2 code: GI Phone code  350
 ISO2 code: ES Phone code  34
 ISO2 code: MV Phone code  960
 ISO2 code: CA Phone code  1
 ISO2 code: SZ Phone code  268
 ISO2 code: SN Phone code  221
 ISO2 code: BB Phone code  +1-246
 ISO2 code: BO Phone code  591
 ISO2 code: HK Phone code  852
 ISO2 code: IQ Phone code  964
 ISO2 code: AI Phone code  +1-264
 ISO2 code: AM Phone code  374
 ISO2 code: AQ Phone code
 ISO2 code: MK Phone code  389
 ISO2 code: FR Phone code  33
 ISO2 code: CG Phone code  242
 ISO2 code: SX Phone code  599
 ISO2 code: DK Phone code  45
 ISO2 code: BE Phone code  32
 ISO2 code: MD Phone code  373
 ISO2 code: NP Phone code  977
 ISO2 code: KI Phone code  686
 ISO2 code: KM Phone code  269
 ISO2 code: BW Phone code  267
 ISO2 code: EH Phone code  212
 ISO2 code: KE Phone code  254
 ISO2 code: IE Phone code  353
 ISO2 code: JE Phone code  +44-1534
 ISO2 code: ET Phone code  251
 ISO2 code: MT Phone code  356
 ISO2 code: KW Phone code  965
 ISO2 code: AU Phone code  61
 ISO2 code: GU Phone code  +1-671
 ISO2 code: VG Phone code  +1-284
 ISO2 code: BM Phone code  +1-441
 ISO2 code: DO Phone code  +1-809 and 1-829
 ISO2 code: FI Phone code  358
 ISO2 code: CI Phone code  225
 ISO2 code: CH Phone code  41
 ISO2 code: CC Phone code  61
 ISO2 code: LK Phone code  94
 ISO2 code: RW Phone code  250
 ISO2 code: KH Phone code  855
 ISO2 code: TL Phone code  670
 ISO2 code: GL Phone code  299
 ISO2 code: PH Phone code  63
 ISO2 code: SA Phone code  966
 ISO2 code: MF Phone code  590
 ISO2 code: CW Phone code  599
 ISO2 code: VA Phone code  379
 ISO2 code: BH Phone code  973
 ISO2 code: IM Phone code  +44-1624
 ISO2 code: IL Phone code  972
 ISO2 code: NG Phone code  234
 ISO2 code: NR Phone code  674
 ISO2 code: AO Phone code  244
 ISO2 code: AG Phone code  +1-268
 ISO2 code: BF Phone code  226
 ISO2 code: WF Phone code  681
 ISO2 code: BQ Phone code  599
 ISO2 code: GR Phone code  30
 ISO2 code: GA Phone code  241
 ISO2 code: OM Phone code  968
 ISO2 code: SO Phone code  252
 ISO2 code: TM Phone code  993
 ISO2 code: UG Phone code  256
 ISO2 code: NO Phone code  47
 ISO2 code: NA Phone code  264
 ISO2 code: HU Phone code  36
 ISO2 code: HM Phone code
 ISO2 code: PL Phone code  48
 ISO2 code: SL Phone code  232
 ISO2 code: HN Phone code  504
 ISO2 code: ZA Phone code  27
 ISO2 code: NZ Phone code  64
 ISO2 code: DJ Phone code  253
 ISO2 code: JP Phone code  81
 ISO2 code: ER Phone code  291
 ISO2 code: VI Phone code  +1-340
 ISO2 code: GB Phone code  44
 ISO2 code: MU Phone code  230
 ISO2 code: ZM Phone code  260
 ISO2 code: NC Phone code  687
 ISO2 code: VC Phone code  +1-784
 ISO2 code: PR Phone code  +1-787 and 1-939
 ISO2 code: MC Phone code  377
 ISO2 code: MZ Phone code  258
 ISO2 code: SK Phone code  421
 ISO2 code: PN Phone code  870
 ISO2 code: VU Phone code  678
 ISO2 code: KY Phone code  +1-345
 ISO2 code: LA Phone code  856
 ISO2 code: AS Phone code  +1-684
 ISO2 code: TN Phone code  216
 ISO2 code: VN Phone code  84
 ISO2 code: FO Phone code  298
 ISO2 code: CK Phone code  682
 ISO2 code: AZ Phone code  994
 ISO2 code: GQ Phone code  240
 ISO2 code: SV Phone code  503
 ISO2 code: TG Phone code  228
 ISO2 code: NL Phone code  31
 ISO2 code: CF Phone code  236
 ISO2 code: CY Phone code  357
 ISO2 code: SM Phone code  378
 ISO2 code: US Phone code  1
 ISO2 code: CU Phone code  53
 ISO2 code: BD Phone code  880
 ISO2 code: BY Phone code  375
 ISO2 code: VE Phone code  58
 ISO2 code: EE Phone code  372
 ISO2 code: CR Phone code  506
 ISO2 code: DM Phone code  +1-767
 ISO2 code: SE Phone code  46
 ISO2 code: UY Phone code  598
 ISO2 code: TW Phone code  886
 ISO2 code: TH Phone code  66
 ISO2 code: AT Phone code  43
 ISO2 code: CD Phone code  243
 ISO2 code: SG Phone code  65
 ISO2 code: TV Phone code  688
 ISO2 code: TO Phone code  676
 ISO2 code: QA Phone code  974
 ISO2 code: BL Phone code  590
 ISO2 code: PT Phone code  351
 ISO2 code: UM Phone code  1
 ISO2 code: TT Phone code  +1-868
 ISO2 code: TD Phone code  235
 ISO2 code: PE Phone code  51
 ISO2 code: NU Phone code  683
 ISO2 code: CO Phone code  57
 ISO2 code: SI Phone code  386
 ISO2 code: YT Phone code  262
 ISO2 code: IR Phone code  98
 ISO2 code: TR Phone code  90
 ISO2 code: BV Phone code
 ISO2 code: HT Phone code  509
 ISO2 code: SB Phone code  677
 ISO2 code: MG Phone code  261
 ISO2 code: MA Phone code  212
 ISO2 code: MQ Phone code  596
 ISO2 code: KP Phone code  850
 ISO2 code: AR Phone code  54
 ISO2 code: XK Phone code
 ISO2 code: LU Phone code  352
 ISO2 code: GN Phone code  224
 ISO2 code: NF Phone code  672
 ISO2 code: SD Phone code  249
 ISO2 code: KZ Phone code  7
 ISO2 code: BZ Phone code  501
 ISO2 code: RU Phone code  7
 ISO2 code: PM Phone code  508
 ISO2 code: ME Phone code  382
 ISO2 code: TZ Phone code  255
 ISO2 code: NI Phone code  505
 ISO2 code: KN Phone code  +1-869
 ISO2 code: RO Phone code  40
 ISO2 code: PW Phone code  680
 ISO2 code: MO Phone code  853
 ISO2 code: LR Phone code  231
 ISO2 code: UA Phone code  380
 ISO2 code: BR Phone code  55
 ISO2 code: GF Phone code  594
 ISO2 code: PK Phone code  92
 ISO2 code: EC Phone code  593
 ISO2 code: CX Phone code  61
 ISO2 code: LC Phone code  +1-758
 ISO2 code: MP Phone code  +1-670
 ISO2 code: CL Phone code  56
 ISO2 code: DE Phone code  49
 ISO2 code: BT Phone code  975
 ISO2 code: GS Phone code
 ISO2 code: MN Phone code  976
 ISO2 code: AW Phone code  297
 ISO2 code: GY Phone code  592
 ISO2 code: SR Phone code  597
 ISO2 code: GD Phone code  +1-473
 ISO2 code: PA Phone code  507
 ISO2 code: CV Phone code  238
 ISO2 code: SY Phone code  963
 ISO2 code: TC Phone code  +1-649
 ISO2 code: BA Phone code  387
 ISO2 code: JO Phone code  962
 ISO2 code: SJ Phone code  47
 ISO2 code: UZ Phone code  998
 ISO2 code: ML Phone code  223
 ISO2 code: MY Phone code  60
 ISO2 code: WS Phone code  685
 ISO2 code: BS Phone code  +1-242
 ISO2 code: TK Phone code  690
 ISO2 code: TJ Phone code  992
 ISO2 code: BJ Phone code  229
 ISO2 code: MW Phone code  265
 ISO2 code: ST Phone code  239
 ISO2 code: LT Phone code  370
 ISO2 code: PG Phone code  675
 ISO2 code: ZW Phone code  263
 ISO2 code: DZ Phone code  213
 ISO2 code: LY Phone code  218
 ISO2 code: GG Phone code  +44-1481
 ISO2 code: MS Phone code  +1-664
 ISO2 code: FM Phone code  691
 ISO2 code: KR Phone code  82
 ISO2 code: AE Phone code  971
 ISO2 code: ID Phone code  62
 ISO2 code: JM Phone code  +1-876
 ISO2 code: RS Phone code  381
 ISO2 code: PY Phone code  595
 ISO2 code: IO Phone code  246
 ISO2 code: FK Phone code  500
 ISO2 code: CZ Phone code  420
 ISO2 code: LI Phone code  423
 ISO2 code: GW Phone code  245
 ISO2 code: GT Phone code  502
 ISO2 code: MH Phone code  692
 ISO2 code: SS Phone code  211
 ISO2 code: LS Phone code  266
 ISO2 code: IT Phone code  39
 ISO2 code: MX Phone code  52
 ISO2 code: IN Phone code  91
 ISO2 code: BG Phone code  359
 ISO2 code: FJ Phone code  679
 ISO2 code: YE Phone code  967
 ISO2 code: LV Phone code  371
 ISO2 code: AL Phone code  355
 ISO2 code: AX Phone code  +358-18

Reference :

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

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



Tags : golang json-unmarshal-to-map countries-phone-codes iso

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