country

package
v0.0.0-...-520e928 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 8 Imported by: 4

Documentation

Overview

Package country provides comprehensive country code handling and validation based on ISO 3166-1 alpha-2 standards for Go applications.

The package includes: - ISO 3166-1 alpha-2 country code validation and normalization - Alternative country code mappings (ITU codes, German names, etc.) - European Union membership checking - Database integration (Scanner/Valuer interfaces) - JSON marshalling/unmarshalling - Nullable country code support

Package country provides comprehensive country code handling and validation based on ISO 3166-1 alpha-2 standards for Go applications.

The package includes: - ISO 3166-1 alpha-2 country code validation and normalization - Alternative country code mappings (ITU codes, German names, etc.) - European Union membership checking - Database integration (Scanner/Valuer interfaces) - JSON marshalling/unmarshalling - Nullable country code support

Package country provides comprehensive country code handling and validation based on ISO 3166-1 alpha-2 standards for Go applications.

The package includes: - ISO 3166-1 alpha-2 country code validation and normalization - Alternative country code mappings (ITU codes, German names, etc.) - European Union membership checking - Database integration (Scanner/Valuer interfaces) - JSON marshalling/unmarshalling - Nullable country code support

Index

Constants

This section is empty.

Variables

View Source
var AltCodes = map[string]Code{

	"A":   AT,
	"DEU": DE,

	"AUT": AT,
	"B":   BR,
	"D":   DE,
	"E":   ES,
	"F":   FR,
	"G":   GB,
	"I":   IT,
	"J":   JP,
	"S":   SE,
	"SUI": CH,

	"ALBANIEN":               AL,
	"ANDORRA":                AD,
	"ARMENIEN":               AM,
	"ASERBAIDSCHAN":          AZ,
	"BELGIEN":                BE,
	"BOSNIEN-HERZEGOWINA":    BA,
	"BULGARIEN":              BG,
	"DÄNEMARK":               DK,
	"DEUTSCHLAND":            DE,
	"ESTLAND":                EE,
	"FINNLAND":               FI,
	"FRANKREICH":             FR,
	"GEORGIEN":               GE,
	"GRIECHENLAND":           GR,
	"IRLAND":                 IE,
	"ISLAND":                 IS,
	"ITALIEN":                IT,
	"KASACHSTAN":             KZ,
	"KOSOVO":                 XK,
	"KROATIEN":               HR,
	"LETTLAND":               LV,
	"LIECHTENSTEIN":          LI,
	"LITAUEN":                LT,
	"LUXEMBURG":              LU,
	"MALTA":                  MT,
	"MOLDAWIEN":              MD,
	"MONACO":                 MC,
	"MONTENEGRO":             ME,
	"NIEDERLANDE":            NL,
	"NORDMAZEDONIEN":         MK,
	"NORWEGEN":               NO,
	"ÖSTERREICH":             AT,
	"OESTERREICH":            AT,
	"POLEN":                  PL,
	"PORTUGAL":               PT,
	"RUMÄNIEN":               RO,
	"RUSSLAND":               RU,
	"SAN MARINO":             SM,
	"SCHWEDEN":               SE,
	"SCHWEIZ":                CH,
	"SERBIEN":                RS,
	"SLOWAKEI":               SK,
	"SLOWENIEN":              SI,
	"SPANIEN":                ES,
	"TSCHECHISCHE REPUBLIK":  CZ,
	"TÜRKEI":                 TR,
	"UKRAINE":                UA,
	"UNGARN":                 HU,
	"VATIKANSTADT":           VA,
	"VEREINIGTES KÖNIGREICH": GB,
	"WEISSRUSSLAND":          BY,
	"ZYPERN":                 CY,
}

AltCodes provides alternative country code mappings. Includes common codes from other standards, ITU letter codes, and German country names.

Functions

This section is empty.

Types

type Code

type Code string

Code represents a country code according to ISO 3166-1 alpha-2 standard. Code implements the database/sql.Scanner and database/sql/driver.Valuer interfaces, and treats an empty Code string as SQL NULL. See NullableCode for a nullable version of this type.

const (
	AF Code = "AF" // Afghanistan
	AX Code = "AX" // Åland Islands
	AL Code = "AL" // Albania
	DZ Code = "DZ" // Algeria
	AS Code = "AS" // American Samoa
	AO Code = "AO" // Angola
	AI Code = "AI" // Anguilla
	AQ Code = "AQ" // Antarctica
	AG Code = "AG" // Antigua and Barbuda
	AR Code = "AR" // Argentina
	AM Code = "AM" // Armenia
	AW Code = "AW" // Aruba
	AU Code = "AU" // Australia
	AT Code = "AT" // Austria
	AZ Code = "AZ" // Azerbaijan
	BS Code = "BS" // Bahamas
	BH Code = "BH" // Bahrain
	BD Code = "BD" // Bangladesh
	BB Code = "BB" // Barbados
	BY Code = "BY" // Belarus
	BE Code = "BE" // Belgium
	BZ Code = "BZ" // Belize
	BJ Code = "BJ" // Benin
	BM Code = "BM" // Bermuda
	BT Code = "BT" // Bhutan
	BO Code = "BO" // Bolivia, Plurinational State of
	BQ Code = "BQ" // Bonaire, Sint Eustatius and Saba
	BA Code = "BA" // Bosnia and Herzegovina
	BW Code = "BW" // Botswana
	BV Code = "BV" // Bouvet Island
	BR Code = "BR" // Brazil
	IO Code = "IO" // British Indian Ocean Territory
	BN Code = "BN" // Brunei Darussalam
	BG Code = "BG" // Bulgaria
	BF Code = "BF" // Burkina Faso
	BI Code = "BI" // Burundi
	KH Code = "KH" // Cambodia
	CM Code = "CM" // Cameroon
	CA Code = "CA" // Canada
	CV Code = "CV" // Cape Verde
	KY Code = "KY" // Cayman Islands
	CF Code = "CF" // Central African Republic
	TD Code = "TD" // Chad
	CL Code = "CL" // Chile
	CN Code = "CN" // China
	CX Code = "CX" // Christmas Island
	CC Code = "CC" // Cocos (Keeling) Islands
	CO Code = "CO" // Colombia
	KM Code = "KM" // Comoros
	CG Code = "CG" // Congo
	CD Code = "CD" // Congo, the Democratic Republic of the
	CK Code = "CK" // Cook Islands
	CR Code = "CR" // Costa Rica
	CI Code = "CI" // Côte d'Ivoire
	HR Code = "HR" // Croatia
	CU Code = "CU" // Cuba
	CW Code = "CW" // Curaçao
	CY Code = "CY" // Cyprus
	CZ Code = "CZ" // Czech Republic
	DK Code = "DK" // Denmark
	DJ Code = "DJ" // Djibouti
	DM Code = "DM" // Dominica
	DO Code = "DO" // Dominican Republic
	EC Code = "EC" // Ecuador
	EG Code = "EG" // Egypt
	SV Code = "SV" // El Salvador
	GQ Code = "GQ" // Equatorial Guinea
	ER Code = "ER" // Eritrea
	EE Code = "EE" // Estonia
	ET Code = "ET" // Ethiopia
	FK Code = "FK" // Falkland Islands (Malvinas)
	FO Code = "FO" // Faroe Islands
	FJ Code = "FJ" // Fiji
	FI Code = "FI" // Finland
	FR Code = "FR" // France
	GF Code = "GF" // French Guiana
	PF Code = "PF" // French Polynesia
	TF Code = "TF" // French Southern Territories
	GA Code = "GA" // Gabon
	GM Code = "GM" // Gambia
	GE Code = "GE" // Georgia
	DE Code = "DE" // Germany
	GH Code = "GH" // Ghana
	GI Code = "GI" // Gibraltar
	GR Code = "GR" // Greece
	EL Code = "EL" // Greece (alternative)
	GL Code = "GL" // Greenland
	GD Code = "GD" // Grenada
	GP Code = "GP" // Guadeloupe
	GU Code = "GU" // Guam
	GT Code = "GT" // Guatemala
	GG Code = "GG" // Guernsey
	GN Code = "GN" // Guinea
	GW Code = "GW" // Guinea-Bissau
	GY Code = "GY" // Guyana
	HT Code = "HT" // Haiti
	HM Code = "HM" // Heard Island and McDonald Islands
	VA Code = "VA" // Holy See (Vatican City State)
	HN Code = "HN" // Honduras
	HK Code = "HK" // Hong Kong
	HU Code = "HU" // Hungary
	IS Code = "IS" // Iceland
	IN Code = "IN" // India
	ID Code = "ID" // Indonesia
	IR Code = "IR" // Iran, Islamic Republic of
	IQ Code = "IQ" // Iraq
	IE Code = "IE" // Ireland
	IM Code = "IM" // Isle of Man
	IL Code = "IL" // Israel
	IT Code = "IT" // Italy
	JM Code = "JM" // Jamaica
	JP Code = "JP" // Japan
	JE Code = "JE" // Jersey
	JO Code = "JO" // Jordan
	KZ Code = "KZ" // Kazakhstan
	KE Code = "KE" // Kenya
	KI Code = "KI" // Kiribati
	KP Code = "KP" // Korea, Democratic People's Republic of
	KR Code = "KR" // Korea, Republic of
	KW Code = "KW" // Kuwait
	KG Code = "KG" // Kyrgyzstan
	LA Code = "LA" // Lao People's Democratic Republic
	LV Code = "LV" // Latvia
	LB Code = "LB" // Lebanon
	LS Code = "LS" // Lesotho
	LR Code = "LR" // Liberia
	LY Code = "LY" // Libya
	LI Code = "LI" // Liechtenstein
	LT Code = "LT" // Lithuania
	LU Code = "LU" // Luxembourg
	MO Code = "MO" // Macao
	MK Code = "MK" // Macedonia, the Former Yugoslav Republic of
	MG Code = "MG" // Madagascar
	MW Code = "MW" // Malawi
	MY Code = "MY" // Malaysia
	MV Code = "MV" // Maldives
	ML Code = "ML" // Mali
	MT Code = "MT" // Malta
	MH Code = "MH" // Marshall Islands
	MQ Code = "MQ" // Martinique
	MR Code = "MR" // Mauritania
	MU Code = "MU" // Mauritius
	YT Code = "YT" // Mayotte
	MX Code = "MX" // Mexico
	FM Code = "FM" // Micronesia, Federated States of
	MD Code = "MD" // Moldova, Republic of
	MC Code = "MC" // Monaco
	MN Code = "MN" // Mongolia
	ME Code = "ME" // Montenegro
	MS Code = "MS" // Montserrat
	MA Code = "MA" // Morocco
	MZ Code = "MZ" // Mozambique
	MM Code = "MM" // Myanmar
	NA Code = "NA" // Namibia
	NR Code = "NR" // Nauru
	NP Code = "NP" // Nepal
	NL Code = "NL" // Netherlands
	NC Code = "NC" // New Caledonia
	NZ Code = "NZ" // New Zealand
	NI Code = "NI" // Nicaragua
	NE Code = "NE" // Niger
	NG Code = "NG" // Nigeria
	NU Code = "NU" // Niue
	NF Code = "NF" // Norfolk Island
	MP Code = "MP" // Northern Mariana Islands
	NO Code = "NO" // Norway
	OM Code = "OM" // Oman
	PK Code = "PK" // Pakistan
	PW Code = "PW" // Palau
	PS Code = "PS" // Palestine, State of
	PA Code = "PA" // Panama
	PG Code = "PG" // Papua New Guinea
	PY Code = "PY" // Paraguay
	PE Code = "PE" // Peru
	PH Code = "PH" // Philippines
	PN Code = "PN" // Pitcairn
	PL Code = "PL" // Poland
	PT Code = "PT" // Portugal
	PR Code = "PR" // Puerto Rico
	QA Code = "QA" // Qatar
	RE Code = "RE" // Réunion
	RO Code = "RO" // Romania
	RU Code = "RU" // Russian Federation
	RW Code = "RW" // Rwanda
	BL Code = "BL" // Saint Barthélemy
	SH Code = "SH" // Saint Helena, Ascension and Tristan da Cunha
	KN Code = "KN" // Saint Kitts and Nevis
	LC Code = "LC" // Saint Lucia
	MF Code = "MF" // Saint Martin (French part)
	PM Code = "PM" // Saint Pierre and Miquelon
	VC Code = "VC" // Saint Vincent and the Grenadines
	WS Code = "WS" // Samoa
	SM Code = "SM" // San Marino
	ST Code = "ST" // Sao Tome and Principe
	SA Code = "SA" // Saudi Arabia
	SN Code = "SN" // Senegal
	RS Code = "RS" // Serbia
	SC Code = "SC" // Seychelles
	SL Code = "SL" // Sierra Leone
	SG Code = "SG" // Singapore
	SX Code = "SX" // Sint Maarten (Dutch part)
	SK Code = "SK" // Slovakia
	SI Code = "SI" // Slovenia
	SB Code = "SB" // Solomon Islands
	SO Code = "SO" // Somalia
	ZA Code = "ZA" // South Africa
	GS Code = "GS" // South Georgia and the South Sandwich Islands
	SS Code = "SS" // South Sudan
	ES Code = "ES" // Spain
	LK Code = "LK" // Sri Lanka
	SD Code = "SD" // Sudan
	SR Code = "SR" // Suriname
	SJ Code = "SJ" // Svalbard and Jan Mayen
	SZ Code = "SZ" // Swaziland
	SE Code = "SE" // Sweden
	CH Code = "CH" // Switzerland
	SY Code = "SY" // Syrian Arab Republic
	TW Code = "TW" // Taiwan, Province of China
	TJ Code = "TJ" // Tajikistan
	TZ Code = "TZ" // Tanzania, United Republic of
	TH Code = "TH" // Thailand
	TL Code = "TL" // Timor-Leste
	TG Code = "TG" // Togo
	TK Code = "TK" // Tokelau
	TO Code = "TO" // Tonga
	TT Code = "TT" // Trinidad and Tobago
	TN Code = "TN" // Tunisia
	TR Code = "TR" // Turkey
	TM Code = "TM" // Turkmenistan
	TC Code = "TC" // Turks and Caicos Islands
	TV Code = "TV" // Tuvalu
	UG Code = "UG" // Uganda
	UA Code = "UA" // Ukraine
	AE Code = "AE" // United Arab Emirates
	GB Code = "GB" // United Kingdom
	US Code = "US" // United States
	UM Code = "UM" // United States Minor Outlying Islands
	UY Code = "UY" // Uruguay
	UZ Code = "UZ" // Uzbekistan
	VU Code = "VU" // Vanuatu
	VE Code = "VE" // Venezuela, Bolivarian Republic of
	VN Code = "VN" // Viet Nam
	VG Code = "VG" // Virgin Islands, British
	VI Code = "VI" // Virgin Islands, U.S.
	WF Code = "WF" // Wallis and Futuna
	EH Code = "EH" // Western Sahara
	YE Code = "YE" // Yemen
	ZM Code = "ZM" // Zambia
	ZW Code = "ZW" // Zimbabwe
	XK Code = "XK" // Republic of Kosovo (unofficial, but still can be used)
)

Country code constants for ISO 3166-1 alpha-2 standard. These represent all officially recognized country codes.

const Invalid Code = ""

Invalid represents an invalid country code.

func (Code) EnglishName

func (c Code) EnglishName() string

EnglishName returns the English name of the country. Returns an empty string if the code is invalid.

func (Code) IsEU

func (c Code) IsEU() bool

IsEU indicates if a country is a member of the European Union.

func (Code) JSONSchema

func (Code) JSONSchema() *jsonschema.Schema

JSONSchema returns the JSON schema definition for the Code type.

func (Code) MarshalJSON

func (c Code) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler. Returns the normalized code as a JSON string.

func (Code) Normalized

func (c Code) Normalized() (Code, error)

Normalized uses the whitespace-trimmed uppercase string of the code to look up and return the standard ISO 3166-1 alpha-2 code. If not found, then AltCodes is used to look up alternative code and name mappings using the whitespace-trimmed uppercase code. If no mapping exists, then the original Code is returned unchanged together with an error.

func (Code) Nullable

func (c Code) Nullable() NullableCode

Nullable returns the Code as NullableCode. Country code Invalid is returned as Null.

func (*Code) Scan

func (c *Code) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*Code) ScanString

func (c *Code) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type. If validate is true, the source string is checked for validity before assignment. If validate is false and the source string can still be assigned in some non-normalized way, it will be assigned without returning an error.

func (Code) String

func (c Code) String() string

String returns the normalized code if possible, else it will be returned unchanged as string.

String implements the fmt.Stringer interface.

func (Code) Valid

func (c Code) Valid() bool

Valid returns true if the normalized Code is valid. See Normalized for the normalization process.

func (Code) ValidAndNormalized

func (c Code) ValidAndNormalized() bool

ValidAndNormalized returns true if the Code is valid and already normalized.

func (Code) Validate

func (c Code) Validate() error

Validate returns an error if the normalized Code is not valid. See Normalized for the normalization process.

func (Code) Value

func (c Code) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface. Returns nil for SQL NULL if the Code is Invalid.

type NullableCode

type NullableCode string

NullableCode represents a country code according to ISO 3166-1 alpha-2 standard where an empty string represents NULL. NullableCode implements the database/sql.Scanner and database/sql/driver.Valuer interfaces, and treats an empty NullableCode string as SQL NULL. Null.Valid() or NullableCode("").Valid() will return true.

const Null NullableCode = ""

Null represents a null country code.

func (NullableCode) EnglishName

func (n NullableCode) EnglishName() string

EnglishName returns the English name of the country.

func (NullableCode) Get

func (n NullableCode) Get() Code

Get returns the non-nullable Code value or panics if the NullableCode is null. Note: check with IsNull before using Get!

func (NullableCode) GetOr

func (n NullableCode) GetOr(defaultCode Code) Code

GetOr returns the non-nullable Code value or the passed defaultCode if the NullableCode is null.

func (NullableCode) IsEU

func (n NullableCode) IsEU() bool

IsEU indicates if a country is a member of the European Union.

func (NullableCode) IsNotNull

func (n NullableCode) IsNotNull() bool

IsNotNull returns true if the NullableCode is not null.

func (NullableCode) IsNull

func (n NullableCode) IsNull() bool

IsNull returns true if the NullableCode is null. IsNull implements the nullable.Nullable interface.

func (NullableCode) JSONSchema

func (NullableCode) JSONSchema() *jsonschema.Schema

JSONSchema returns the JSON schema definition for the NullableCode type.

func (NullableCode) MarshalJSON

func (n NullableCode) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler by returning the JSON null value for an empty (null) string.

func (NullableCode) Normalized

func (n NullableCode) Normalized() (NullableCode, error)

Normalized uses the whitespace-trimmed uppercase string of the code to look up and return the standard ISO 3166-1 alpha-2 code, or return Null and no error in case of an empty string representing a null value. If not found, then AltCodes is used to look up alternative code and name mappings using the whitespace-trimmed uppercase code. If no mapping exists, then the original Code is returned unchanged together with an error.

func (NullableCode) NormalizedOrNull

func (n NullableCode) NormalizedOrNull() NullableCode

NormalizedOrNull returns the normalized NullableCode or Null if normalization fails.

func (NullableCode) NormalizedWithAltCodes

func (n NullableCode) NormalizedWithAltCodes() (NullableCode, error)

NormalizedWithAltCodes uses AltCodes to map to ISO 3166-1 alpha-2 codes or returns the result of Normalized() if no mapping exists.

func (*NullableCode) Scan

func (n *NullableCode) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*NullableCode) ScanString

func (n *NullableCode) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type. If validate is true, the source string is checked for validity before assignment. If validate is false and the source string can still be assigned in some non-normalized way, it will be assigned without returning an error.

func (*NullableCode) Set

func (n *NullableCode) Set(code Code)

Set sets a Code for this NullableCode.

func (*NullableCode) SetNull

func (n *NullableCode) SetNull()

SetNull sets the NullableCode to null.

func (NullableCode) String

func (n NullableCode) String() string

String returns the normalized code if possible, else it will be returned unchanged as string. String implements the fmt.Stringer interface.

func (NullableCode) StringOr

func (n NullableCode) StringOr(defaultString string) string

StringOr returns the NullableCode as string or the passed defaultString if the NullableCode is null.

func (NullableCode) Valid

func (n NullableCode) Valid() bool

Valid returns true if the NullableCode is valid (including null values).

func (NullableCode) ValidAndNormalized

func (n NullableCode) ValidAndNormalized() bool

ValidAndNormalized returns true if the NullableCode is valid and already normalized.

func (NullableCode) ValidAndNotNull

func (n NullableCode) ValidAndNotNull() bool

ValidAndNotNull returns true if the NullableCode is valid and not null.

func (NullableCode) Validate

func (n NullableCode) Validate() error

Validate returns an error if the NullableCode is not valid.

func (NullableCode) Value

func (n NullableCode) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface. Returns nil for SQL NULL if the NullableCode is Null.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL