countrycode

countrycode provides access to ISO-3166 country metadata.

Part of nurago, a collection of independent Go packages for backend services.

import "github.com/tecnickcom/nurago/pkg/countrycode"

Package countrycode provides access to ISO-3166 country metadata.

It translates country identifiers into structured records and selects countries by geographic hierarchy, assignment status, or top-level domain.

CountryData includes:

  • ISO-3166 alpha-2, alpha-3, and numeric codes
  • English and French country names
  • region, sub-region, and intermediate region names and codes
  • assignment status and top-level domain (TLD)

Data sources and initialization

New(nil) builds a Data instance from embedded defaults sourced from ISO-3166, the CIA World Factbook, United Nations M49, and Wikipedia. New also accepts a custom []CountryData dataset for private overrides, curated subsets, or pinned metadata snapshots.

Typical usage

Create a resolver with New, then retrieve a single country with CountryByAlpha2Code, CountryByAlpha3Code, or CountryByNumericCode, or fetch filtered sets using CountriesByRegionCode, CountriesByStatusName, and related query methods.

When To Use

  • You validate or expand country codes.
  • The data should be compiled in rather than fetched.

Example

data, err := countrycode.New(nil)
if err != nil {
	log.Fatal(err)
}

got, err := data.CountryByAlpha2Code("ZW")
if err != nil {
	log.Fatal(err)
}

b, err := json.MarshalIndent(got, "", "  ")
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(b))

// Output:
// {
//   "statusCode": 1,
//   "status": "Officially assigned",
//   "alpha2Code": "ZW",
//   "alpha3Code": "ZWE",
//   "numericCode": "716",
//   "nameEnglish": "Zimbabwe",
//   "nameFrench": "Zimbabwe (le)",
//   "region": "Africa",
//   "subRegion": "Sub-Saharan Africa",
//   "intermediateRegion": "Eastern Africa",
//   "regionCode": "002",
//   "subRegionCode": "202",
//   "intermediateRegionCode": "014",
//   "tld": "zw"
// }

Full source is in example_countrycode_test.go. More runnable examples are on pkg.go.dev.

Dependencies

This package reaches no external module: it uses only the Go standard library.