countrycode

package module
v0.0.0-...-af66c0c Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2019 License: MIT Imports: 1 Imported by: 0

README

go-countrycode

CI status GoDoc

Package countrycode provides utilities for representing countries in code, and handling their serializations and deserializations in a convenient way.

All deserializations will result in CountryUndefined if input data is not a recognized country code.

All the exported types are one word in memory and as such provide fast equality checks, hashing for usage as keys in maps. Conversions between the types are zero overhead (don't even escape the stack), serialization is worst case O(1), and deserialization is worst case O(n).

License

MIT License. See LICENSE for more details.

Documentation

Overview

Package countrycode provides utilities for representing countries in code, and handling their serializations and deserializations in a convenient way.

All deserializations will result in `CountryUndefined` if input data is not a recognized country code.

All the exported types are one word in memory and as such provide fast equality checks, hashing for usage as keys in maps. Conversions between the types are zero overhead (don't even escape the stack), serialization is worst case O(1), and deserialization is worst case O(n).

Example (MarshalJSON)
mapping := map[countrycode.CountryAlpha3]int{
	countrycode.USA.Alpha3(): 1234,
	countrycode.FIN.Alpha3(): 2345,
}
data, _ := json.Marshal(mapping)
Output:

{"FIN":2345,"USA":1234}
Example (UnmarshalJSON)
data := []byte(`
		{
			"USA": 1234,
			"FIN": 2345
		}
	`)
var mapping map[countrycode.CountryAlpha3]int
_ = json.Unmarshal(data, &mapping)
Output:

map[countrycode.CountryAlpha3]int{FIN:2345, USA:1234}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ABW is Aruba.
	ABW = Country{1}
	// AFG is Afghanistan.
	AFG = Country{2}
	// AGO is Angola.
	AGO = Country{3}
	// AIA is Anguilla.
	AIA = Country{4}
	// ALA is Åland Islands.
	ALA = Country{5}
	// ALB is Albania.
	ALB = Country{6}
	// AND is Andorra.
	AND = Country{7}
	// ARE is United Arab Emirates.
	ARE = Country{8}
	// ARG is Argentina.
	ARG = Country{9}
	// ARM is Armenia.
	ARM = Country{10}
	// ASM is American Samoa.
	ASM = Country{11}
	// ATA is Antarctica.
	ATA = Country{12}
	// ATF is French Southern Territories.
	ATF = Country{13}
	// ATG is Antigua and Barbuda.
	ATG = Country{14}
	// AUS is Australia.
	AUS = Country{15}
	// AUT is Austria.
	AUT = Country{16}
	// AZE is Azerbaijan.
	AZE = Country{17}
	// BDI is Burundi.
	BDI = Country{18}
	// BEL is Belgium.
	BEL = Country{19}
	// BEN is Benin.
	BEN = Country{20}
	// BES is Bonaire, Sint Eustatius and Saba.
	BES = Country{21}
	// BFA is Burkina Faso.
	BFA = Country{22}
	// BGD is Bangladesh.
	BGD = Country{23}
	// BGR is Bulgaria.
	BGR = Country{24}
	// BHR is Bahrain.
	BHR = Country{25}
	// BHS is Bahamas.
	BHS = Country{26}
	// BIH is Bosnia and Herzegovina.
	BIH = Country{27}
	// BLM is Saint Barthélemy.
	BLM = Country{28}
	// BLR is Belarus.
	BLR = Country{29}
	// BLZ is Belize.
	BLZ = Country{30}
	// BMU is Bermuda.
	BMU = Country{31}
	// BOL is Bolivia (Plurinational State of).
	BOL = Country{32}
	// BRA is Brazil.
	BRA = Country{33}
	// BRB is Barbados.
	BRB = Country{34}
	// BRN is Brunei Darussalam.
	BRN = Country{35}
	// BTN is Bhutan.
	BTN = Country{36}
	// BVT is Bouvet Island.
	BVT = Country{37}
	// BWA is Botswana.
	BWA = Country{38}
	// CAF is Central African Republic.
	CAF = Country{39}
	// CAN is Canada.
	CAN = Country{40}
	// CCK is Cocos (Keeling) Islands.
	CCK = Country{41}
	// CHE is Switzerland.
	CHE = Country{42}
	// CHL is Chile.
	CHL = Country{43}
	// CHN is China.
	CHN = Country{44}
	// CIV is Côte d'Ivoire.
	CIV = Country{45}
	// CMR is Cameroon.
	CMR = Country{46}
	// COD is Congo, Democratic Republic of the.
	COD = Country{47}
	// COG is Congo.
	COG = Country{48}
	// COK is Cook Islands.
	COK = Country{49}
	// COL is Colombia.
	COL = Country{50}
	// COM is Comoros.
	COM = Country{51}
	// CPV is Cabo Verde.
	CPV = Country{52}
	// CRI is Costa Rica.
	CRI = Country{53}
	// CUB is Cuba.
	CUB = Country{54}
	// CUW is Curaçao.
	CUW = Country{55}
	// CXR is Christmas Island.
	CXR = Country{56}
	// CYM is Cayman Islands.
	CYM = Country{57}
	// CYP is Cyprus.
	CYP = Country{58}
	// CZE is Czechia.
	CZE = Country{59}
	// DEU is Germany.
	DEU = Country{60}
	// DJI is Djibouti.
	DJI = Country{61}
	// DMA is Dominica.
	DMA = Country{62}
	// DNK is Denmark.
	DNK = Country{63}
	// DOM is Dominican Republic.
	DOM = Country{64}
	// DZA is Algeria.
	DZA = Country{65}
	// ECU is Ecuador.
	ECU = Country{66}
	// EGY is Egypt.
	EGY = Country{67}
	// ERI is Eritrea.
	ERI = Country{68}
	// ESH is Western Sahara.
	ESH = Country{69}
	// ESP is Spain.
	ESP = Country{70}
	// EST is Estonia.
	EST = Country{71}
	// ETH is Ethiopia.
	ETH = Country{72}
	// FIN is Finland.
	FIN = Country{73}
	// FJI is Fiji.
	FJI = Country{74}
	// FLK is Falkland Islands (Malvinas).
	FLK = Country{75}
	// FRA is France.
	FRA = Country{76}
	// FRO is Faroe Islands.
	FRO = Country{77}
	// FSM is Micronesia (Federated States of).
	FSM = Country{78}
	// GAB is Gabon.
	GAB = Country{79}
	// GBR is United Kingdom of Great Britain and Northern Ireland.
	GBR = Country{80}
	// GEO is Georgia.
	GEO = Country{81}
	// GGY is Guernsey.
	GGY = Country{82}
	// GHA is Ghana.
	GHA = Country{83}
	// GIB is Gibraltar.
	GIB = Country{84}
	// GIN is Guinea.
	GIN = Country{85}
	// GLP is Guadeloupe.
	GLP = Country{86}
	// GMB is Gambia.
	GMB = Country{87}
	// GNB is Guinea-Bissau.
	GNB = Country{88}
	// GNQ is Equatorial Guinea.
	GNQ = Country{89}
	// GRC is Greece.
	GRC = Country{90}
	// GRD is Grenada.
	GRD = Country{91}
	// GRL is Greenland.
	GRL = Country{92}
	// GTM is Guatemala.
	GTM = Country{93}
	// GUF is French Guiana.
	GUF = Country{94}
	// GUM is Guam.
	GUM = Country{95}
	// GUY is Guyana.
	GUY = Country{96}
	// HKG is Hong Kong.
	HKG = Country{97}
	// HMD is Heard Island and McDonald Islands.
	HMD = Country{98}
	// HND is Honduras.
	HND = Country{99}
	// HRV is Croatia.
	HRV = Country{100}
	// HTI is Haiti.
	HTI = Country{101}
	// HUN is Hungary.
	HUN = Country{102}
	// IDN is Indonesia.
	IDN = Country{103}
	// IMN is Isle of Man.
	IMN = Country{104}
	// IND is India.
	IND = Country{105}
	// IOT is British Indian Ocean Territory.
	IOT = Country{106}
	// IRL is Ireland.
	IRL = Country{107}
	// IRN is Iran (Islamic Republic of).
	IRN = Country{108}
	// IRQ is Iraq.
	IRQ = Country{109}
	// ISL is Iceland.
	ISL = Country{110}
	// ISR is Israel.
	ISR = Country{111}
	// ITA is Italy.
	ITA = Country{112}
	// JAM is Jamaica.
	JAM = Country{113}
	// JEY is Jersey.
	JEY = Country{114}
	// JOR is Jordan.
	JOR = Country{115}
	// JPN is Japan.
	JPN = Country{116}
	// KAZ is Kazakhstan.
	KAZ = Country{117}
	// KEN is Kenya.
	KEN = Country{118}
	// KGZ is Kyrgyzstan.
	KGZ = Country{119}
	// KHM is Cambodia.
	KHM = Country{120}
	// KIR is Kiribati.
	KIR = Country{121}
	// KNA is Saint Kitts and Nevis.
	KNA = Country{122}
	// KOR is Korea, Republic of.
	KOR = Country{123}
	// KWT is Kuwait.
	KWT = Country{124}
	// LAO is Lao People's Democratic Republic.
	LAO = Country{125}
	// LBN is Lebanon.
	LBN = Country{126}
	// LBR is Liberia.
	LBR = Country{127}
	// LBY is Libya.
	LBY = Country{128}
	// LCA is Saint Lucia.
	LCA = Country{129}
	// LIE is Liechtenstein.
	LIE = Country{130}
	// LKA is Sri Lanka.
	LKA = Country{131}
	// LSO is Lesotho.
	LSO = Country{132}
	// LTU is Lithuania.
	LTU = Country{133}
	// LUX is Luxembourg.
	LUX = Country{134}
	// LVA is Latvia.
	LVA = Country{135}
	// MAC is Macao.
	MAC = Country{136}
	// MAF is Saint Martin (French part).
	MAF = Country{137}
	// MAR is Morocco.
	MAR = Country{138}
	// MCO is Monaco.
	MCO = Country{139}
	// MDA is Moldova, Republic of.
	MDA = Country{140}
	// MDG is Madagascar.
	MDG = Country{141}
	// MDV is Maldives.
	MDV = Country{142}
	// MEX is Mexico.
	MEX = Country{143}
	// MHL is Marshall Islands.
	MHL = Country{144}
	// MKD is North Macedonia.
	MKD = Country{145}
	// MLI is Mali.
	MLI = Country{146}
	// MLT is Malta.
	MLT = Country{147}
	// MMR is Myanmar.
	MMR = Country{148}
	// MNE is Montenegro.
	MNE = Country{149}
	// MNG is Mongolia.
	MNG = Country{150}
	// MNP is Northern Mariana Islands.
	MNP = Country{151}
	// MOZ is Mozambique.
	MOZ = Country{152}
	// MRT is Mauritania.
	MRT = Country{153}
	// MSR is Montserrat.
	MSR = Country{154}
	// MTQ is Martinique.
	MTQ = Country{155}
	// MUS is Mauritius.
	MUS = Country{156}
	// MWI is Malawi.
	MWI = Country{157}
	// MYS is Malaysia.
	MYS = Country{158}
	// MYT is Mayotte.
	MYT = Country{159}
	// NAM is Namibia.
	NAM = Country{160}
	// NCL is New Caledonia.
	NCL = Country{161}
	// NER is Niger.
	NER = Country{162}
	// NFK is Norfolk Island.
	NFK = Country{163}
	// NGA is Nigeria.
	NGA = Country{164}
	// NIC is Nicaragua.
	NIC = Country{165}
	// NIU is Niue.
	NIU = Country{166}
	// NLD is Netherlands.
	NLD = Country{167}
	// NOR is Norway.
	NOR = Country{168}
	// NPL is Nepal.
	NPL = Country{169}
	// NRU is Nauru.
	NRU = Country{170}
	// NZL is New Zealand.
	NZL = Country{171}
	// OMN is Oman.
	OMN = Country{172}
	// PAK is Pakistan.
	PAK = Country{173}
	// PAN is Panama.
	PAN = Country{174}
	// PCN is Pitcairn.
	PCN = Country{175}
	// PER is Peru.
	PER = Country{176}
	// PHL is Philippines.
	PHL = Country{177}
	// PLW is Palau.
	PLW = Country{178}
	// PNG is Papua New Guinea.
	PNG = Country{179}
	// POL is Poland.
	POL = Country{180}
	// PRI is Puerto Rico.
	PRI = Country{181}
	// PRK is Korea (Democratic People's Republic of).
	PRK = Country{182}
	// PRT is Portugal.
	PRT = Country{183}
	// PRY is Paraguay.
	PRY = Country{184}
	// PSE is Palestine, State of.
	PSE = Country{185}
	// PYF is French Polynesia.
	PYF = Country{186}
	// QAT is Qatar.
	QAT = Country{187}
	// REU is Réunion.
	REU = Country{188}
	// ROU is Romania.
	ROU = Country{189}
	// RUS is Russian Federation.
	RUS = Country{190}
	// RWA is Rwanda.
	RWA = Country{191}
	// SAU is Saudi Arabia.
	SAU = Country{192}
	// SDN is Sudan.
	SDN = Country{193}
	// SEN is Senegal.
	SEN = Country{194}
	// SGP is Singapore.
	SGP = Country{195}
	// SGS is South Georgia and the South Sandwich Islands.
	SGS = Country{196}
	// SHN is Saint Helena, Ascension and Tristan da Cunha.
	SHN = Country{197}
	// SJM is Svalbard and Jan Mayen.
	SJM = Country{198}
	// SLB is Solomon Islands.
	SLB = Country{199}
	// SLE is Sierra Leone.
	SLE = Country{200}
	// SLV is El Salvador.
	SLV = Country{201}
	// SMR is San Marino.
	SMR = Country{202}
	// SOM is Somalia.
	SOM = Country{203}
	// SPM is Saint Pierre and Miquelon.
	SPM = Country{204}
	// SRB is Serbia.
	SRB = Country{205}
	// SSD is South Sudan.
	SSD = Country{206}
	// STP is Sao Tome and Principe.
	STP = Country{207}
	// SUR is Suriname.
	SUR = Country{208}
	// SVK is Slovakia.
	SVK = Country{209}
	// SVN is Slovenia.
	SVN = Country{210}
	// SWE is Sweden.
	SWE = Country{211}
	// SWZ is Eswatini.
	SWZ = Country{212}
	// SXM is Sint Maarten (Dutch part).
	SXM = Country{213}
	// SYC is Seychelles.
	SYC = Country{214}
	// SYR is Syrian Arab Republic.
	SYR = Country{215}
	// TCA is Turks and Caicos Islands.
	TCA = Country{216}
	// TCD is Chad.
	TCD = Country{217}
	// TGO is Togo.
	TGO = Country{218}
	// THA is Thailand.
	THA = Country{219}
	// TJK is Tajikistan.
	TJK = Country{220}
	// TKL is Tokelau.
	TKL = Country{221}
	// TKM is Turkmenistan.
	TKM = Country{222}
	// TLS is Timor-Leste.
	TLS = Country{223}
	// TON is Tonga.
	TON = Country{224}
	// TTO is Trinidad and Tobago.
	TTO = Country{225}
	// TUN is Tunisia.
	TUN = Country{226}
	// TUR is Turkey.
	TUR = Country{227}
	// TUV is Tuvalu.
	TUV = Country{228}
	// TWN is Taiwan, Province of China.
	TWN = Country{229}
	// TZA is Tanzania, United Republic of.
	TZA = Country{230}
	// UGA is Uganda.
	UGA = Country{231}
	// UKR is Ukraine.
	UKR = Country{232}
	// UMI is United States Minor Outlying Islands.
	UMI = Country{233}
	// URY is Uruguay.
	URY = Country{234}
	// USA is United States of America.
	USA = Country{235}
	// UZB is Uzbekistan.
	UZB = Country{236}
	// VAT is Holy See.
	VAT = Country{237}
	// VCT is Saint Vincent and the Grenadines.
	VCT = Country{238}
	// VEN is Venezuela (Bolivarian Republic of).
	VEN = Country{239}
	// VGB is Virgin Islands (British).
	VGB = Country{240}
	// VIR is Virgin Islands (U.S.).
	VIR = Country{241}
	// VNM is Viet Nam.
	VNM = Country{242}
	// VUT is Vanuatu.
	VUT = Country{243}
	// WLF is Wallis and Futuna.
	WLF = Country{244}
	// WSM is Samoa.
	WSM = Country{245}
	// YEM is Yemen.
	YEM = Country{246}
	// ZAF is South Africa.
	ZAF = Country{247}
	// ZMB is Zambia.
	ZMB = Country{248}
	// ZWE is Zimbabwe.
	ZWE = Country{249}
)

Code generated. DO NOT EDIT.

View Source
var CountryUndefined = Country{}

CountryUndefined represents an undefined country.

Functions

This section is empty.

Types

type Country

type Country struct {
	// contains filtered or unexported fields
}

Country represents the country codes of a country.

func (Country) Alpha2

func (c Country) Alpha2() CountryAlpha2

Alpha2 returns the Country in a CountryAlpha2 serializer.

func (Country) Alpha2LowerCase

func (c Country) Alpha2LowerCase() CountryAlpha2LowerCase

Alpha2LowerCase returns the Country in a CountryAlpha2LowerCase serializer.

func (Country) Alpha3

func (c Country) Alpha3() CountryAlpha3

Alpha3 returns the Country in a CountryAlpha3 serializer.

func (Country) GoString

func (c Country) GoString() string

GoString implements fmt.GoStringer for debugging purposes.

type CountryAlpha2

type CountryAlpha2 struct {
	Country
}

CountryAlpha2 is a Country in ISO-3166-1 Alpha-2 format.

func (CountryAlpha2) GoString

func (c CountryAlpha2) GoString() string

GoString implements fmt.GoStringer.

func (CountryAlpha2) MarshalText

func (c CountryAlpha2) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (CountryAlpha2) String

func (c CountryAlpha2) String() string

String implements fmt.Stringer.

func (*CountryAlpha2) UnmarshalText

func (c *CountryAlpha2) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type CountryAlpha2LowerCase

type CountryAlpha2LowerCase struct {
	Country
}

CountryAlpha2LowerCase is a Country in lowercase ISO-3166-1 Alpha-2 format.

func (CountryAlpha2LowerCase) GoString

func (c CountryAlpha2LowerCase) GoString() string

GoString implements fmt.GoStringer.

func (CountryAlpha2LowerCase) MarshalText

func (c CountryAlpha2LowerCase) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (CountryAlpha2LowerCase) String

func (c CountryAlpha2LowerCase) String() string

String implements fmt.Stringer.

func (*CountryAlpha2LowerCase) UnmarshalText

func (c *CountryAlpha2LowerCase) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type CountryAlpha3

type CountryAlpha3 struct {
	Country
}

CountryAlpha3 is a Country in ISO-3166-1 Alpha-3 format.

func (CountryAlpha3) GoString

func (c CountryAlpha3) GoString() string

GoString implements fmt.GoStringer.

func (CountryAlpha3) MarshalText

func (c CountryAlpha3) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (CountryAlpha3) String

func (c CountryAlpha3) String() string

String implements fmt.Stringer.

func (*CountryAlpha3) UnmarshalText

func (c *CountryAlpha3) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Format

type Format int

Format represents a specific country code format with a specific serialization.

Example (TwoWayConversion)
const countryCodeFormat = countrycode.FormatAlpha3

type externalData struct {
	Country string
}

type internalData struct {
	Country countrycode.Country
}

toInternalData := func(d externalData) internalData {
	return internalData{Country: countryCodeFormat.Deserialize(d.Country)}
}

toExternalData := func(d internalData) externalData {
	return externalData{Country: countryCodeFormat.Serialize(d.Country)}
}

edata := externalData{Country: "FIN"}
idata := toInternalData(edata)
edata = toExternalData(idata)
Output:

{FIN}
const (
	// FormatAlpha3 is an ISO-3166-1 Alpha-3 country code.
	FormatAlpha3 Format = iota
	// FormatAlpha2 is an ISO-3166-1 Alpha-2 country code.
	FormatAlpha2
	// FormatAlpha2LowerCase is a lowercased ISO-3166-1 Alpha-2 country code.
	FormatAlpha2LowerCase
)

func (Format) Deserialize

func (f Format) Deserialize(countryCode string) Country

Deserialize the specified country code string of the Format into a Country.

Example
country := countrycode.FormatAlpha2.Deserialize("US")
Output:

true

func (Format) Serialize

func (f Format) Serialize(country Country) string

Serialize the specified Country into a country code string of the Format.

Example
countryCodeAlpha2 := countrycode.FormatAlpha2.Serialize(countrycode.USA)
Output:

US

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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