The go-iso
package is a Go library that provides ISO 3166-1 country codes and associated data. It allows you to retrieve country information by name, alpha-2 code, alpha-3 code, or numeric code.
- Comprehensive list of countries compliant with the ISO 3166-1 standard.
- Efficient lookup functions to retrieve country data based on different code formats.
- Easy integration into Go projects for internationalization and localization needs.
To install the package, run:
go get github.com/emil-petras/go-iso
First, import the package into your Go project:
import (
"fmt"
iso31661 "github.com/emil-petras/go-iso/iso3166-1"
)
country := goiso.GetByName("Japan")
if country != nil {
fmt.Printf("Country: %s, Alpha-2 Code: %s\n", country.Name, country.Alpha2)
} else {
fmt.Println("Country not found")
}
country := goiso.GetByAlpha2("JP")
if country != nil {
fmt.Printf("Country: %s, Numeric Code: %s\n", country.Name, country.Numeric)
} else {
fmt.Println("Country not found")
}
country := goiso.GetByAlpha3("JPN")
if country != nil {
fmt.Printf("Country: %s, Alpha-2 Code: %s\n", country.Name, country.Alpha2)
} else {
fmt.Println("Country not found")
}
country := goiso.GetByNumeric("392")
if country != nil {
fmt.Printf("Country: %s, Alpha-3 Code: %s\n", country.Name, country.Alpha3)
} else {
fmt.Println("Country not found")
}
The Country
type is defined in the go-iso
(root) package and has the following structure:
type Country struct {
Name string
Alpha2 string
Alpha3 string
Numeric string
}
Upon package initialization, the country data is loaded into maps for efficient retrieval:
countryByName
countryByAlpha2
countryByAlpha3
countryByNumeric
This allows constant-time lookups when using the provided getter functions.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes with clear messages.
- Submit a pull request.
This package aims to keep the country data up-to-date with the ISO 3166-1 standard. However, always verify the data for critical applications.
For any issues or questions, please open an issue on the GitHub repository.