ISO 3166 - Country Codes
The iso3166 package provides typed constants for ISO 3166-1 country codes.
Types
Alpha2
Two-letter country code (e.g., US, DE, GB).
go
type Alpha2 stringMethods:
| Method | Description |
|---|---|
String() string | Returns the string representation |
Valid() bool | Returns true if the code is a valid ISO 3166-1 alpha-2 code |
Alpha3
Three-letter country code (e.g., USA, DEU, GBR).
go
type Alpha3 stringMethods:
| Method | Description |
|---|---|
String() string | Returns the string representation |
Valid() bool | Returns true if the code is a valid ISO 3166-1 alpha-3 code |
Usage
go
package main
import (
"fmt"
"github.com/foomo/gostandards/iso3166"
)
func main() {
// Alpha-2 codes
country := iso3166.Alpha2DE
fmt.Println(country) // "DE"
fmt.Println(country.Valid()) // true
// Alpha-3 codes
country3 := iso3166.Alpha3DEU
fmt.Println(country3) // "DEU"
fmt.Println(country3.Valid()) // true
// Validation
invalid := iso3166.Alpha2("XX")
fmt.Println(invalid.Valid()) // false
}Constants
Constants follow the naming convention Alpha2XX and Alpha3XXX, where the suffix is the country code itself:
go
iso3166.Alpha2US // "US" - United States of America
iso3166.Alpha2GB // "GB" - United Kingdom
iso3166.Alpha2DE // "DE" - Germany
iso3166.Alpha2FR // "FR" - France
iso3166.Alpha2JP // "JP" - Japan
iso3166.Alpha3USA // "USA" - United States of America
iso3166.Alpha3GBR // "GBR" - United Kingdom
iso3166.Alpha3DEU // "DEU" - GermanyThe full list contains 250 country codes. See the source code for all constants.
Code Generation
The constants are generated from official ISO data sources. Do not edit the generated files directly — run make generate to regenerate them.
