Go package providing comprehensive country data in all standard ISO formats.
CI / CD Β Β
|
|
Β Β Β Β Quality Β Β
|
|
Security Β Β
|
|
Β Β Β Β Community Β Β
|
|
πΒ Installation
|
π§ͺΒ ExamplesΒ &Β Tests
|
πΒ Documentation
|
π€Β Contributing
|
π οΈΒ CodeΒ Standards
|
β‘Β Benchmarks
|
π€Β AIΒ Usage
|
βοΈΒ License
|
π₯Β Maintainers
|
go-countries requires a supported release of Go.
go get -u github.com/mrz1836/go-countriesHere is a basic example of how to use go-countries in your Go project:
package main
import (
"fmt"
"github.com/mrz1836/go-countries"
)
func main() {
country := countries.GetByAlpha2(countries.Alpha2US)
if country != nil {
fmt.Printf("Country: %s, Capital: %s\n", country.Name, country.Capital)
} else {
fmt.Println("Country not found")
}
}- Explore additional usage examples for practical integration patterns
- Review benchmark results to assess performance characteristics
- Examine the comprehensive test suite for validation and coverage
- Inspect the code generator for details on data sources and the build process
View the generated documentation
Heads up!
go-countriesis intentionally light on dependencies. The only external package it uses is the excellenttestifysuiteβand that's just for our tests. You can drop this library into your projects without dragging along extra baggage.
- Comprehensive list of all recognized countries, provided as a Go slice of structs for easy access and manipulation
- Direct access to raw country and currency JSON data in the data package for custom processing or validation
- Zero
init()overheadβjust import and use thecountriespackage without side effects - Fast, allocation-free lookups for all retrieval functions, ensuring optimal performance in production environments
- Includes region, subregion, capital, and currency information for each country
- Designed for extensibilityβadd or update country data via code generation from JSON sources
- Well-documented, tested, and benchmarked for reliability and speed
GetAll(): Retrieve the entire slice of all known countries, including metadata such as names, codes, regions, capitals, and currenciesGetByAlpha2(countries.Alpha2US): Find a country by its ISO 3166-1 alpha-2 codeGetByAlpha3(countries.Alpha3USA): Retrieve a country by its ISO 3166-1 alpha-3 codeGetByCapital("Washington"): Find a country by its capital city in a case-insensitive searchGetByCountryCode("840"): Lookup by ISO 3166 numeric country code, supporting string or integer inputGetByISO31662("ISO 3166-2:US"): Retrieve a country by its ISO 3166-2 subdivision codeGetByName("United States of America"): Lookup a country by its official name, supporting case-insensitive queries
If you need to update the country data or regenerate the Go code, you can use the go generate command.
This will read the JSON data files and generate a Go file containing all the country data in a structured format.
To generate the Go code for the country data, follow these steps:
-
Navigate to the project root directory:
cd /path/to/your/project -
Run the
go generatecommand:go generate -x ./generate/...
This command executes the code generation logic defined in the generate.go file located in the /generate/ directory.
The generated code is written to countries_data.go in the project directory.
Development Setup (Getting Started)
Install MAGE-X build tool for development:
# Install MAGE-X for development and building
go install github.com/mrz1836/mage-x/cmd/magex@latest
magex update:installLibrary Deployment
This project uses goreleaser for streamlined binary and library deployment to GitHub. To get started, install it via:
brew install goreleaserThe release process is defined in the .goreleaser.yml configuration file.
Then create and push a new Git tag using:
magex version:bump bump=patch push=true branch=masterThis process ensures consistent, repeatable releases with properly versioned artifacts and citation metadata.
Build Commands
View all build commands
magex helpGitHub Workflows
All workflows are driven by modular configuration in .github/env/ β no YAML editing required.
Updating Dependencies
To update all dependencies (Go modules, linters, and related tools), run:
magex deps:updateThis command ensures all dependencies are brought up to date in a single step, including Go modules and any managed tools. It is the recommended way to keep your development environment and CI in sync with the latest versions.
All unit tests and examples run via GitHub Actions and use Go version 1.22.x. View the configuration file.
Run all tests (fast):
magex testRun all tests with race detector (slower):
magex test:raceRun the Go benchmarks:
magex benchPerformance benchmarks for the core functions in this library, executed on an Apple M1 Max (ARM64):
| Function | Ops/sec (approx) | Time per op | Allocations | Bytes Allocated |
|---|---|---|---|---|
GetAll |
5,263,413 | 225.4 ns | 1 | 2,048 B |
GetByAlpha2 |
100,000,000 | 10.36 ns | 0 | 0 B |
GetByAlpha3 |
100,000,000 | 10.97 ns | 0 | 0 B |
GetByCapital |
33,426,183 | 35.19 ns | 1 | 16 B |
GetByCountryCode |
100,467,049 | 11.10 ns | 0 | 0 B |
GetByISO31662 |
59,682,316 | 20.16 ns | 0 | 0 B |
GetByName |
46,466,527 | 25.66 ns | 0 | 0 B |
These benchmarks reflect fast, allocation-free lookups for most retrieval functions, ensuring optimal performance in production environments.
Read more about this Go project's code standards.
Read the AI Usage & Assistant Guidelines for details on how AI is used in this project and how to interact with AI assistants.
Used: ISO-3166-Countries-with-Regional-Codes Used: List of Countries & Currencies
![]() |
|---|
| MrZ |
View the contributing guidelines and please follow the code of conduct.
All kinds of contributions are welcome π! The most basic way to show your support is to star π the project, or to raise issues π¬. You can also support this project by becoming a sponsor on GitHub π or by making a bitcoin donation to ensure this journey continues indefinitely! π
