countries

repository·master·Indexed 19 days ago

https://github.com/biter777/countries

A high-performance Go library providing comprehensive country and currency data based on international standards including ISO 3166, ISO 4217, and ITU-T E.164. It features zero external dependencies and provides metadata for country codes, currencies, calling codes, and capitals. The library is compatible with SQL databases, GORM, JSON, BSON, GOB, and XML.

Tokens
8.1K
Snippets
53
Records
55
Agent score
68%

What's inside countries

  1. Access country data and metadata

    master

    You can access country constants directly (e.g., countries.Japan) or use lookup functions like ByName and ByNumeric.

    Once you have a country object, you can retrieve various ISO and regional data using method calls. The package also provides an .Info() method that returns a struct containing most of these fields for easier access to non-method properties.

    // Accessing via constants and methods
    countryJapan := countries.Japan
    fmt.Printf("Name: %v\n", countryJapan)                   // Japan
    fmt.Printf("Alpha-2: %v\n", countryJapan.Alpha2())    // JP
    fmt.Printf("Capital: %v\n", countryJapan.Capital())   // Tokyo
    
    // Accessing via Info() struct
    japanInfo := countries.Japan.Info()
    fmt.Printf("Name: %v\n", japanInfo.Name)               // Japan
    fmt.Printf("Alpha-2: %v\n", japanInfo.Alpha2)        // JP
  2. Retrieve country information and metadata

    master

    You can access detailed country information using two different patterns:

    1. Method Pattern: Call methods directly on a CountryCode to get specific attributes (e.g., .Alpha2(), .Capital(), .Emoji()).
    2. Info Pattern: Call .Info() on a CountryCode to receive a Country struct containing all metadata fields at once.

    Available Metadata Fields (via Country struct):

    • Name: English name
    • Alpha2: ISO 3166-1 Alpha-2 code
    • Alpha3: ISO 3166-1 Alpha-3 code
    • FIPS: FIPS code
    • IOC: IOC/NOC code
    • FIFA: FIFA code
    • Emoji: Country flag emoji
    • Code: Numeric CountryCode
    • Currency: Associated CurrencyCode
    • Capital: Capital city code
    • CallCodes: ITU-T E.164 calling codes
    • Domain: ccTLD domain
    • Region: UN M.49 region code
    • Subdivisions: List of subdivision codes
    // Method pattern
    fmt.Println(countries.Japan.Alpha2()) // JP
    fmt.Println(countries.Japan.Emoji()) // 🇯🇵
    
    // Info pattern (returns a Country struct)
    info := countries.Japan.Info()
    fmt.Println(info.Name)     // Japan
    fmt.Println(info.Capital) // Tokyo
  3. Use countries in databases (GORM/SQL)

    master

    The package is compatible with Databases, JSON, BSON, GOB, and XML. You can use countries.CountryCode and countries.CurrencyCode as types within your database models (e.g., GORM structs) to store country and currency information efficiently.

    type User struct {
    	gorm.Model
    	Name     string
    	Country  countries.CountryCode
    	Currency countries.CurrencyCode
    }
    
    user := &User{Name: "Helen", Country: countries.Slovenia, Currency: countries.CurrencyEUR}
    // ... save user to DB
  4. Work with currencies and country associations

    master

    You can derive currency information from a country object using .Currency(). This returns a currency object which provides its own metadata (Alpha code, emoji, etc.) and allows you to find which countries use that specific currency via .Countries().

    currencyJapan := countries.Japan.Currency()
    fmt.Printf("Currency Name: %v\n", currencyJapan)      // Yen
    fmt.Printf("Currency Alpha: %v\n", currencyJapan.Alpha()) // JPY
    fmt.Printf("Currency Emoji: %v\n", currencyJapan.Emoji()) // 💴
    
    // Find all countries using this currency
    fmt.Printf("Countries: %v\n", currencyJapan.Countries())
  5. Lookup countries by name or code

    master

    Use the following functions to find a country object based on different identifiers:

    • ByName(name string): Look up by English name or ISO Alpha-2/Alpha-3 code.
    • ByNumeric(code int): Look up by ISO 3166-1 numeric code.
    // Lookup by name
    country := countries.ByName("angola")
    
    // Lookup by Alpha-2 code
    country = countries.ByName("AO")
    
    // Lookup by numeric code
    country = countries.ByNumeric(24)
  6. Scan and Value for CallCodeInfo in SQL databases

    master

    The CallCodeInfo struct implements the database/sql/driver.Valuer and database/sql.Scanner interfaces. This allows you to store CallCodeInfo directly in database columns as JSON.

    • Value(): Marshals the CallCodeInfo to JSON.
    • Scan(src interface{}): Unmarshals JSON from the database back into a CallCodeInfo struct.
  7. Generate country flag emojis

    master

    You can generate Unicode flag emojis directly from a CountryCode using:

    • .Emoji(): Returns the flag based on the ISO Alpha-2 code.
    • .Emoji3(): Returns a flag representation based on the ISO Alpha-3 code.
    country := countries.Japan
    fmt.Println(country.Emoji())  // 🇯🇵
    fmt.Println(country.Emoji3()) // 🇯🇵
  8. Use CurrencyCode to access ISO 4217 data

    master

    The CurrencyCode type (an int64) is the primary way to interact with currency data. You can use it to retrieve the currency's name, ISO 3166-1 Alpha code, decimal digits, associated countries, and emoji representations.

    Key methods available on CurrencyCode:

    • String(): Returns the English name of the currency.
    • Alpha(): Returns the 3-character ISO 3166-1 Alpha code.
    • Digits(): Returns the number of decimal digits used by the currency.
    • Emoji(): Returns a currency emoji (for USD, EUR, JPY, and GBP) or the Alpha code otherwise.
    • Countries(): Returns a slice of CountryCode associated with the currency.
    • NickelRounding(): Returns true if the currency uses nickel rounding (e.g., CAD, DKK, CHF).
    • Info(): Returns a pointer to a Currency struct containing all the above information.
    // Example usage of CurrencyCode methods
    code := countries.CurrencyUSD
    
    fmt.Println(code.String())   // "US Dollar"
    fmt.Println(code.Alpha())    // "USD"
    fmt.Println(code.Digits())   // 2
    fmt.Println(code.Emoji())    // "💵"
    fmt.Println(code.Countries()) // []countries.CountryCode{...}
  9. Use CallCode to handle ITU-T E.164 calling codes

    master

    The CallCode type represents a country's calling phone code. It is an int64 type, making it compatible with database/sql drivers. You can convert a CallCode to its string representation (e.g., +44) using the .String() method.

    // Example: Converting a CallCode to a string
    var code countries.CallCode = countries.CallCode44
    fmt.Println(code.String()) // Output: +44
  10. Use Domain for database operations

    master

    The Domain struct implements standard Go database interfaces, allowing it to be used directly in SQL queries with database/sql:

    • Value(): Implements driver.Valuer by marshaling the Domain struct to JSON.
    • Scan(src interface{}): Implements sql.Scanner to allow scanning database values (as *Domain or Domain) back into the struct.
    // Example: Scanning a domain from a database row
    var d countries.Domain
    err := row.Scan(&d)
    if err != nil {
        // handle error
    }
  11. Lookup a RegionCode by name

    master

    The RegionCodeByName function allows you to resolve a RegionCode from a string. The lookup is case-insensitive and supports various aliases and abbreviations.

    Example lookups:

    • "EU" or "Europe" $\rightarrow$ RegionEU
    • "AF" or "Africa" $\rightarrow$ RegionAF
    • "NONE" or "XX" $\rightarrow$ RegionNone
    regionEU := countries.RegionCodeByName("europe")
    regionAF := countries.RegionCodeByName("AF")
    
    if regionEU.IsValid() {
        fmt.Println("Found region:", regionEU.String())
    }