Golang : Display list of countries and ISO codes
The code example below will download the JSON formatted data from http://country.io/names.json
and unmarshal the data to display a list of all countries in the world.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
// NOTE : struct doesn't work here due to the nature
// of the JSON data from country.io
// it has no field identifiers
//type Country struct {
// ISO string
//}
func main() {
resp, err := http.Get("http://country.io/names.json")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
jsonCountriesData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//fmt.Println(string(jsonData))
countriesMap := make(map[string]interface{})
//NOTE : unmarshal to struct will throw this error message
// json: cannot unmarshal object into Go value of type
// Decode JSON into our map
err = json.Unmarshal([]byte(jsonCountriesData), &countriesMap)
if err != nil {
println(err)
return
}
for iso2, name := range countriesMap {
fmt.Println("ISO2 code:", iso2, "Country name :", name)
}
}
and the output :
ISO2 code: VA Country name : Vatican
ISO2 code: AW Country name : Aruba
ISO2 code: TN Country name : Tunisia
ISO2 code: MC Country name : Monaco
ISO2 code: SD Country name : Sudan
ISO2 code: TC Country name : Turks and Caicos Islands
ISO2 code: AL Country name : Albania
ISO2 code: PL Country name : Poland
ISO2 code: LT Country name : Lithuania
ISO2 code: LU Country name : Luxembourg
ISO2 code: TH Country name : Thailand
ISO2 code: PA Country name : Panama
ISO2 code: PK Country name : Pakistan
ISO2 code: YE Country name : Yemen
ISO2 code: BG Country name : Bulgaria
ISO2 code: SN Country name : Senegal
ISO2 code: PT Country name : Portugal
ISO2 code: ML Country name : Mali
ISO2 code: MP Country name : Northern Mariana Islands
ISO2 code: SZ Country name : Swaziland
ISO2 code: BI Country name : Burundi
ISO2 code: GM Country name : Gambia
ISO2 code: PR Country name : Puerto Rico
ISO2 code: FK Country name : Falkland Islands
ISO2 code: CN Country name : China
ISO2 code: KY Country name : Cayman Islands
ISO2 code: TK Country name : Tokelau
ISO2 code: HK Country name : Hong Kong
ISO2 code: SO Country name : Somalia
ISO2 code: EG Country name : Egypt
ISO2 code: US Country name : United States
ISO2 code: PN Country name : Pitcairn
ISO2 code: VC Country name : Saint Vincent and the Grenadines
ISO2 code: BQ Country name : Bonaire, Saint Eustatius and Saba
ISO2 code: MK Country name : Macedonia
ISO2 code: TV Country name : Tuvalu
ISO2 code: RE Country name : Reunion
ISO2 code: FR Country name : France
ISO2 code: CX Country name : Christmas Island
ISO2 code: KP Country name : North Korea
ISO2 code: DO Country name : Dominican Republic
ISO2 code: TW Country name : Taiwan
ISO2 code: IS Country name : Iceland
ISO2 code: GT Country name : Guatemala
ISO2 code: PE Country name : Peru
ISO2 code: FM Country name : Micronesia
ISO2 code: IN Country name : India
ISO2 code: SG Country name : Singapore
ISO2 code: MZ Country name : Mozambique
ISO2 code: BT Country name : Bhutan
ISO2 code: GP Country name : Guadeloupe
ISO2 code: MY Country name : Malaysia
ISO2 code: BM Country name : Bermuda
ISO2 code: AI Country name : Anguilla
ISO2 code: QA Country name : Qatar
ISO2 code: SS Country name : South Sudan
ISO2 code: WS Country name : Samoa
ISO2 code: HN Country name : Honduras
ISO2 code: VU Country name : Vanuatu
ISO2 code: FO Country name : Faroe Islands
ISO2 code: TR Country name : Turkey
ISO2 code: BH Country name : Bahrain
ISO2 code: BS Country name : Bahamas
ISO2 code: FJ Country name : Fiji
ISO2 code: MS Country name : Montserrat
ISO2 code: CH Country name : Switzerland
ISO2 code: AD Country name : Andorra
ISO2 code: GA Country name : Gabon
ISO2 code: EC Country name : Ecuador
ISO2 code: MA Country name : Morocco
ISO2 code: MM Country name : Myanmar
ISO2 code: TZ Country name : Tanzania
ISO2 code: SH Country name : Saint Helena
ISO2 code: SX Country name : Sint Maarten
ISO2 code: LA Country name : Laos
ISO2 code: BL Country name : Saint Barthelemy
ISO2 code: RS Country name : Serbia
ISO2 code: MG Country name : Madagascar
ISO2 code: AQ Country name : Antarctica
ISO2 code: BZ Country name : Belize
ISO2 code: ZW Country name : Zimbabwe
ISO2 code: KH Country name : Cambodia
ISO2 code: GD Country name : Grenada
ISO2 code: AM Country name : Armenia
ISO2 code: GW Country name : Guinea-Bissau
ISO2 code: NO Country name : Norway
ISO2 code: PH Country name : Philippines
ISO2 code: AR Country name : Argentina
ISO2 code: BV Country name : Bouvet Island
ISO2 code: ER Country name : Eritrea
ISO2 code: DK Country name : Denmark
ISO2 code: BB Country name : Barbados
ISO2 code: SA Country name : Saudi Arabia
ISO2 code: NA Country name : Namibia
ISO2 code: UY Country name : Uruguay
ISO2 code: TT Country name : Trinidad and Tobago
ISO2 code: AO Country name : Angola
ISO2 code: IL Country name : Israel
ISO2 code: KM Country name : Comoros
ISO2 code: KZ Country name : Kazakhstan
ISO2 code: JP Country name : Japan
ISO2 code: IQ Country name : Iraq
ISO2 code: VN Country name : Vietnam
ISO2 code: SB Country name : Solomon Islands
ISO2 code: MH Country name : Marshall Islands
ISO2 code: CZ Country name : Czech Republic
ISO2 code: KW Country name : Kuwait
ISO2 code: SV Country name : El Salvador
ISO2 code: PM Country name : Saint Pierre and Miquelon
ISO2 code: IT Country name : Italy
ISO2 code: HR Country name : Croatia
ISO2 code: HT Country name : Haiti
ISO2 code: MF Country name : Saint Martin
ISO2 code: UZ Country name : Uzbekistan
ISO2 code: CO Country name : Colombia
ISO2 code: BR Country name : Brazil
ISO2 code: GY Country name : Guyana
ISO2 code: GL Country name : Greenland
ISO2 code: VG Country name : British Virgin Islands
ISO2 code: CU Country name : Cuba
ISO2 code: ST Country name : Sao Tome and Principe
ISO2 code: GG Country name : Guernsey
ISO2 code: PF Country name : French Polynesia
ISO2 code: ZA Country name : South Africa
ISO2 code: FI Country name : Finland
ISO2 code: YT Country name : Mayotte
ISO2 code: TO Country name : Tonga
ISO2 code: AG Country name : Antigua and Barbuda
ISO2 code: JE Country name : Jersey
ISO2 code: RU Country name : Russia
ISO2 code: MQ Country name : Martinique
ISO2 code: NZ Country name : New Zealand
ISO2 code: ES Country name : Spain
ISO2 code: MW Country name : Malawi
ISO2 code: MR Country name : Mauritania
ISO2 code: ID Country name : Indonesia
ISO2 code: PW Country name : Palau
ISO2 code: MO Country name : Macao
ISO2 code: LI Country name : Liechtenstein
ISO2 code: MN Country name : Mongolia
ISO2 code: NL Country name : Netherlands
ISO2 code: CL Country name : Chile
ISO2 code: KG Country name : Kyrgyzstan
ISO2 code: SR Country name : Suriname
ISO2 code: BO Country name : Bolivia
ISO2 code: BJ Country name : Benin
ISO2 code: SJ Country name : Svalbard and Jan Mayen
ISO2 code: KI Country name : Kiribati
ISO2 code: IE Country name : Ireland
ISO2 code: GU Country name : Guam
ISO2 code: KN Country name : Saint Kitts and Nevis
ISO2 code: DJ Country name : Djibouti
ISO2 code: XK Country name : Kosovo
ISO2 code: AX Country name : Aland Islands
ISO2 code: RW Country name : Rwanda
ISO2 code: CM Country name : Cameroon
ISO2 code: AF Country name : Afghanistan
ISO2 code: MT Country name : Malta
ISO2 code: CW Country name : Curacao
ISO2 code: SI Country name : Slovenia
ISO2 code: LK Country name : Sri Lanka
ISO2 code: AS Country name : American Samoa
ISO2 code: WF Country name : Wallis and Futuna
ISO2 code: BW Country name : Botswana
ISO2 code: MU Country name : Mauritius
ISO2 code: SL Country name : Sierra Leone
ISO2 code: LR Country name : Liberia
ISO2 code: MV Country name : Maldives
ISO2 code: DZ Country name : Algeria
ISO2 code: UA Country name : Ukraine
ISO2 code: BA Country name : Bosnia and Herzegovina
ISO2 code: GF Country name : French Guiana
ISO2 code: GN Country name : Guinea
ISO2 code: AZ Country name : Azerbaijan
ISO2 code: NR Country name : Nauru
ISO2 code: KR Country name : South Korea
ISO2 code: SM Country name : San Marino
ISO2 code: SK Country name : Slovakia
ISO2 code: GH Country name : Ghana
ISO2 code: PY Country name : Paraguay
ISO2 code: MD Country name : Moldova
ISO2 code: SY Country name : Syria
ISO2 code: GR Country name : Greece
ISO2 code: NI Country name : Nicaragua
ISO2 code: BY Country name : Belarus
ISO2 code: TL Country name : East Timor
ISO2 code: RO Country name : Romania
ISO2 code: EE Country name : Estonia
ISO2 code: NU Country name : Niue
ISO2 code: DM Country name : Dominica
ISO2 code: BN Country name : Brunei
ISO2 code: TD Country name : Chad
ISO2 code: ZM Country name : Zambia
ISO2 code: LC Country name : Saint Lucia
ISO2 code: ME Country name : Montenegro
ISO2 code: CA Country name : Canada
ISO2 code: KE Country name : Kenya
ISO2 code: NP Country name : Nepal
ISO2 code: CR Country name : Costa Rica
ISO2 code: BE Country name : Belgium
ISO2 code: VI Country name : U.S. Virgin Islands
ISO2 code: IR Country name : Iran
ISO2 code: LB Country name : Lebanon
ISO2 code: AE Country name : United Arab Emirates
ISO2 code: VE Country name : Venezuela
ISO2 code: UG Country name : Uganda
ISO2 code: OM Country name : Oman
ISO2 code: CF Country name : Central African Republic
ISO2 code: UM Country name : United States Minor Outlying Islands
ISO2 code: GQ Country name : Equatorial Guinea
ISO2 code: GB Country name : United Kingdom
ISO2 code: GI Country name : Gibraltar
ISO2 code: GE Country name : Georgia
ISO2 code: CI Country name : Ivory Coast
ISO2 code: TF Country name : French Southern Territories
ISO2 code: IM Country name : Isle of Man
ISO2 code: MX Country name : Mexico
ISO2 code: NF Country name : Norfolk Island
ISO2 code: CV Country name : Cape Verde
ISO2 code: TJ Country name : Tajikistan
ISO2 code: PS Country name : Palestinian Territory
ISO2 code: ET Country name : Ethiopia
ISO2 code: BF Country name : Burkina Faso
ISO2 code: HU Country name : Hungary
ISO2 code: LY Country name : Libya
ISO2 code: BD Country name : Bangladesh
ISO2 code: JO Country name : Jordan
ISO2 code: CG Country name : Republic of the Congo
ISO2 code: CD Country name : Democratic Republic of the Congo
ISO2 code: SE Country name : Sweden
ISO2 code: DE Country name : Germany
ISO2 code: LV Country name : Latvia
ISO2 code: JM Country name : Jamaica
ISO2 code: TM Country name : Turkmenistan
ISO2 code: HM Country name : Heard Island and McDonald Islands
ISO2 code: NE Country name : Niger
ISO2 code: AT Country name : Austria
ISO2 code: EH Country name : Western Sahara
ISO2 code: CY Country name : Cyprus
ISO2 code: PG Country name : Papua New Guinea
ISO2 code: IO Country name : British Indian Ocean Territory
ISO2 code: CK Country name : Cook Islands
ISO2 code: NC Country name : New Caledonia
ISO2 code: SC Country name : Seychelles
ISO2 code: AU Country name : Australia
ISO2 code: LS Country name : Lesotho
ISO2 code: TG Country name : Togo
ISO2 code: GS Country name : South Georgia and the South Sandwich Islands
ISO2 code: NG Country name : Nigeria
ISO2 code: CC Country name : Cocos Islands
References :
See also : Golang : Decode/unmarshal unknown JSON data type with map[string]interface
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
Tutorials
+8.6k Golang : Gaussian blur on image and camera video feed examples
+14.8k Golang : How do I get the local IP (non-loopback) address ?
+20.4k Golang : Convert date string to variants of time.Time type examples
+51.9k Golang : How to get struct field and value by name
+10.3k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+10.9k Golang : Intercept and process UNIX signals example
+22.4k Golang : Round float to precision example
+7.9k Golang : Add build version and other information in executables
+8.1k Golang: Prevent over writing file with md5 hash
+12.7k Golang : Convert(cast) uintptr to string example
+8.9k Android Studio : Indicate progression with ProgressBar example
+8.5k Golang : Accept any number of function arguments with three dots(...)