Country
This package provides functions to search for country information (ISO 3166-1) based on their country codes (Alpha-2, Alpha-3, Numeric) and names. It is designed for easy integration into applications that need to work with country codes or names.
Versions
Installation
To use this package in your project, add it using Go modules:
go get -u github.com/gouef/country
Usage
Country
Struct
Each country is represented by the Country
struct, which contains the following fields:
Name
: The country’s name (in English).Alpha2
: The two-letter country code (ISO 3166-1 alpha-2).Alpha3
: The three-letter country code (ISO 3166-1 alpha-3).Numeric
: The numeric country code (ISO 3166-1 numeric).
Functions
FindByAlpha2(alpha2 string) *Country
This function searches for a country by its two-letter Alpha-2 code.
package main
import "github.com/gouef/country"
sCountry := country.FindByAlpha2("CZ")
if sCountry != nil {
fmt.Println("Country Name:", sCountry.Name)
fmt.Println("Alpha-2 Code:", sCountry.Alpha2)
}
FindByAlpha3(alpha3 string) *Country
This function searches for a country by its three-letter Alpha-3 code.
package main
import "github.com/gouef/country"
sCountry := country.FindByAlpha3("CZE")
if sCountry != nil {
fmt.Println("Country Name:", sCountry.Name)
fmt.Println("Alpha-3 Code:", sCountry.Alpha3)
}
FindByName *Country
This function searches for a country by its name (english).
package main
import "github.com/gouef/country"
sCountry := country.FindByName("Czechia")
if sCountry != nil {
fmt.Println("Country Name:", sCountry.Name)
fmt.Println("Alpha-2 Code:", sCountry.Alpha2)
fmt.Println("Alpha-3 Code:", sCountry.Alpha3)
}
Example
package main
import (
"fmt"
"github.com/gouef/country"
)
func main() {
// Search by Alpha-2 code
sCountry := country.FindByAlpha2("CZ")
if sCountry != nil {
fmt.Println("Found country:", sCountry.Name)
}
// Search by Alpha-3 code
sCountry = country.FindByAlpha3("CZE")
if sCountry != nil {
fmt.Println("Found country:", sCountry.Name)
}
// Search by name
sCountry = country.FindByName("Czechia")
if sCountry != nil {
fmt.Println("Found country:", sCountry.Name)
}
}
Contributing
Read Contributing