### Compare Countries by Code and Name in Go Source: https://github.com/biter777/countries/blob/master/README.md This example illustrates how to compare countries using the Go `countries` library. It shows two methods: comparing by numeric code using `countries.ByName()` and comparing by string name using `strings.EqualFold` against the country's string representation. The `countries` and `strings` packages are required. ```go import ( "fmt" "strings" "github.com/biter777/countries" ) func main() { // Comparing usage // Compare by code/numeric if countries.ByName("angola") == countries.AGO { fmt.Println("Yes! It's Angola!") } // Compare by name if strings.EqualFold("angola", countries.AGO.String()) { fmt.Println("Yes! It's Angola!") } } ``` -------------------------------- ### Detecting/Looking Up Countries by Name or Code in Go Source: https://github.com/biter777/countries/blob/master/README.md This snippet demonstrates how to detect or look up country information using the `ByName` function from the countries package. It shows examples of searching by a full country name (e.g., "angola") and by an ISO Alpha-2 code (e.g., "AO"). The output includes the country's name, digit code, and Alpha-2 and Alpha-3 ISO codes. ```go // Detection/Lookup usage // Detect/Lookup by country name country := countries.ByName("angola") fmt.Printf("Country name in english: %v\n", country) // Angola fmt.Printf("Country ISO-3166 digit code: %d\n", country) // 24 fmt.Printf("Country ISO-3166 Alpha-2 code: %v\n", country.Alpha2()) // AO fmt.Printf("Country ISO-3166 Alpha-3 code: %v\n", country.Alpha3()) // AGO // Detect/Lookup by country code country = countries.ByName("AO") fmt.Printf("Country name in english: %v\n", country.String()) // Angola fmt.Printf("Country ISO-3166 digit code: %d\n", country) // 24 fmt.Printf("Country ISO-3166 Alpha-2 code: %v\n", country.Alpha2()) // AO fmt.Printf("Country ISO-3166 Alpha-3 code: %v\n", country.Alpha3()) // AGO ``` -------------------------------- ### Detect and Lookup Country by Numeric Code in Go Source: https://github.com/biter777/countries/blob/master/README.md This snippet demonstrates how to use the `countries.ByNumeric()` function to retrieve country information based on its numeric ISO code. It then shows how to access the country's name, numeric code, and Alpha-2 and Alpha-3 ISO codes. The `countries` package is a required dependency. ```go import ( "fmt" "github.com/biter777/countries" ) func main() { // Detect/Lookup by code/numeric country := countries.ByNumeric(24) fmt.Printf("Country name in english: %v\n", country) fmt.Printf("Country ISO-3166 digit code: %d\n", country) fmt.Printf("Country ISO-3166 Alpha-2 code: %v\n", country.Alpha2()) fmt.Printf("Country ISO-3166 Alpha-3 code: %v\n", country.Alpha3()) } ``` -------------------------------- ### Database Usage with GORM and Go Countries Source: https://github.com/biter777/countries/blob/master/README.md This snippet demonstrates how to integrate the Go `countries` library with GORM for database operations. It defines a `User` struct that includes `CountryCode` and `CurrencyCode` from the `countries` package and shows how to open a PostgreSQL connection, create a user record, and handle potential errors. The `gorm` and `countries` packages are necessary. ```go import ( "github.com/biter777/countries" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" ) type User struct { gorm.Model Name string Country countries.CountryCode Currency countries.CurrencyCode } func main() { user := &User{Name: "Helen", Country: countries.Slovenia, Currency: countries.CurrencyEUR} db, err := gorm.Open("postgres", "host=127.0.0.2 port=5432 user=usr password=1234567 dbname=db") if err != nil { panic(err) } defer db.Close() db.Create(user) } ``` -------------------------------- ### Access Country Capital City Information Source: https://context7.com/biter777/countries/llms.txt This code snippet shows how to access capital city information for countries using the countries library. It demonstrates retrieving the capital from a country, looking up a capital by its name, and getting detailed information about a capital city, including its associated country. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Get capital from country japan := countries.Japan capital := japan.Capital() fmt.Printf("Capital: %v\n", capital) // Tokyo // More examples france := countries.France fmt.Printf("France capital: %v\n", france.Capital()) // Paris usa := countries.USA fmt.Printf("USA capital: %v\n", usa.Capital()) // Washington // Lookup capital by name capital = countries.CapitalCodeByName("London") fmt.Printf("Capital: %v\n", capital) // London // Get capital info capitalInfo := capital.Info() fmt.Printf("Name: %v\n", capitalInfo.Name) // London fmt.Printf("Country: %v\n", capitalInfo.Country) // United Kingdom } ``` -------------------------------- ### Accessing Japan Country Data in Go Source: https://github.com/biter777/countries/blob/master/README.md This snippet shows how to access detailed information for Japan using predefined constants from the countries package. It demonstrates retrieving the country's name in English and Russian, ISO codes (digit, Alpha-2, Alpha-3), IOC code, FIFA code, FIPS code, capital city, call codes, domain, region, emoji flag, and subdivisions. It also covers accessing currency details like name, digit code, Alpha code, and emoji, along with listing countries associated with that currency. An alternative method using the `.Info()` function is also presented. ```go countryJapan := countries.Japan fmt.Printf("Country name in english: %v\n", countryJapan) // Japan fmt.Printf("Country name in russian: %v\n", countryJapan.StringRus()) // Япония fmt.Printf("Country ISO-3166 digit code: %d\n", countryJapan) // 392 fmt.Printf("Country ISO-3166 Alpha-2 code: %v\n", countryJapan.Alpha2()) // JP fmt.Printf("Country ISO-3166 Alpha-3 code: %v\n", countryJapan.Alpha3()) // JPN fmt.Printf("Country IOC/NOC code: %v\n", countryJapan.IOC()) // JPN fmt.Printf("Country FIFA code: %v\n", countryJapan.FIFA()) // JPN fmt.Printf("Country FIPS code: %v\n", countryJapan.FIPS()) // JA fmt.Printf("Country Capital: %v\n", countryJapan.Capital()) // Tokyo fmt.Printf("Country ITU-T E.164 call code: %v\n", countryJapan.CallCodes()) // +81 fmt.Printf("Country ccTLD domain: %v\n", countryJapan.Domain()) // .jp fmt.Printf("Country UN M.49 region name: %v\n", countryJapan.Region()) // Asia fmt.Printf("Country UN M.49 region code: %d\n", countryJapan.Region()) // 142 fmt.Printf("Country emoji/flag: %v\n", countryJapan.Emoji()) // 🇯🇵 fmt.Printf("Country Subdivisions: %v\n", countryJapan.Subdivisions()) // Hokkaido Aomori Iwate Miyagi Akita Yamagata Fukushima Ibaraki Tochigi Gunma Saitama Chiba Tokyo Kanagawa Niigata Toyama Ishikawa Fukui Yamanashi Nagano Gifu Shizuoka Aichi Mie Shiga Kyoto Osaka Hyogo Nara Wakayama Tottori Shimane Okayama Hiroshima Yamaguchi Tokushima Kagawa Ehime Kochi Fukuoka Saga Nagasaki Kumamoto Oita Miyazaki Kagoshima Okinawa currencyJapan := countryJapan.Currency() fmt.Printf("Country ISO-4217 Currency name in english: %v\n", currencyJapan) // Yen fmt.Printf("Country ISO-4217 Currency digit code: %d\n", currencyJapan) // 392 fmt.Printf("Country ISO-4217 Currency Alpha code: %v\n", currencyJapan.Alpha()) // JPY fmt.Printf("Country Currency emoji: %v\n", currencyJapan.Emoji()) // 💴 fmt.Printf("Country of Currency %v: %v\n\n", currencyJapan, currencyJapan.Countries()) // Japan // OR you can alternative use: japanInfo := countries.Japan.Info() fmt.Printf("Country name in english: %v\n", japanInfo.Name) // Japan fmt.Printf("Country ISO-3166 digit code: %d\n", japanInfo.Code) // 392 fmt.Printf("Country ISO-3166 Alpha-2 code: %v\n", japanInfo.Alpha2) // JP fmt.Printf("Country ISO-3166 Alpha-3 code: %v\n", japanInfo.Alpha3) // JPN fmt.Printf("Country IOC/NOC code: %v\n", japanInfo.IOC) // JPN fmt.Printf("Country FIFA code: %v\n", japanInfo.FIFA) // JPN fmt.Printf("Country FIPS code: %v\n", japanInfo.FIPS) // JA fmt.Printf("Country Capital: %v\n", japanInfo.Capital) // Tokyo fmt.Printf("Country ITU-T E.164 call code: %v\n", japanInfo.CallCodes) // +81 fmt.Printf("Country ccTLD domain: %v\n", japanInfo.Domain) // .jp fmt.Printf("Country UN M.49 region name: %v\n", japanInfo.Region) // Asia fmt.Printf("Country UN M.49 region code: %d\n", japanInfo.Region) // 142 fmt.Printf("Country emoji/flag: %v\n", japanInfo.Emoji) // 🇯🇵 fmt.Printf("Country ISO-4217 Currency name in english: %v\n", japanInfo.Currency) // Yen fmt.Printf("Country ISO-4217 Currency digit code: %d\n", japanInfo.Currency) // 392 fmt.Printf("Country ISO-4217 Currency Alpha code: %v\n", japanInfo.Currency.Alpha()) // JPY fmt.Printf("Country Subdivisions: %v\n", japanInfo.Subdivisions) // Hokkaido Aomori Iwate Miyagi Akita Yamagata Fukushima Ibaraki Tochigi Gunma Saitama Chiba Tokyo Kanagawa Niigata Toyama Ishikawa Fukui Yamanashi Nagano Gifu Shizuoka Aichi Mie Shiga Kyoto Osaka Hyogo Nara Wakayama Tottori Shimane Okayama Hiroshima Yamaguchi Tokushima Kagawa Ehime Kochi Fukuoka Saga Nagasaki Kumamoto Oita Miyazaki Kagoshima Okinawa ``` -------------------------------- ### Access Country Library Statistics and Bulk Data in Go Source: https://context7.com/biter777/countries/llms.txt This Go code snippet shows how to retrieve various statistics and bulk data from the countries library. It includes fetching total counts for countries, currencies, regions, call codes, and domains. It also demonstrates how to get slices of all countries, detailed country information, all currencies, all regions, and domain information, providing a comprehensive overview of the library's contents. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Total counts (fast constant values) fmt.Printf("Total countries: %d\n", countries.Total()) // 252 fmt.Printf("Total currencies: %d\n", countries.TotalCurrencies()) // 169 fmt.Printf("Total regions: %d\n", countries.TotalRegions()) // 7 fmt.Printf("Total call codes: %d\n", countries.TotalCallCodes()) // 264 fmt.Printf("Total domains: %d\n", countries.TotalDomains()) // 263 // Get all countries allCountries := countries.All() fmt.Printf("Countries array length: %d\n", len(allCountries)) // Get all as detailed info allInfo := countries.AllInfo() for i, info := range allInfo[:3] { fmt.Printf("%d. %s (%s) - %s\n", i+1, info.Name, info.Alpha2, info.Capital) } // Get all currencies allCurrencies := countries.AllCurrencies() fmt.Printf("Currencies array length: %d\n", len(allCurrencies)) // Get all regions allRegions := countries.AllRegions() for _, region := range allRegions { fmt.Printf("Region: %v (%d)\n", region, region) } // Get all domains info allDomains := countries.AllDomainsInfo() fmt.Printf("First domain: %v for %v\n", allDomains[0].Name, allDomains[0].Country) } ``` -------------------------------- ### Work with Country Domain Codes (IANA ccTLD) Source: https://context7.com/biter777/countries/llms.txt This snippet demonstrates how to retrieve and validate country domain codes (ccTLDs) using the countries library. It covers getting the domain from a country, looking up a country by domain, and accessing domain information. It also shows how to handle generic TLDs like .com. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Get domain from country japan := countries.Japan domain := japan.Domain() fmt.Printf("Domain: %v\n", domain) // .jp fmt.Printf("Valid: %v\n", domain.IsValid()) // true // Get country from domain country := domain.Country() fmt.Printf("Country: %v\n", country) // Japan // Lookup domain by name domain = countries.DomainCodeByName(".uk") fmt.Printf("Domain: %v\n", domain) // .uk domain = countries.DomainCodeByName("de") fmt.Printf("Domain: %v\n", domain) // .de // Get domain info domainInfo := domain.Info() fmt.Printf("Name: %v\n", domainInfo.Name) // .de fmt.Printf("Country: %v\n", domainInfo.Country) // Germany // Generic TLDs com := countries.DomainCom fmt.Printf("Domain: %v\n", com) // .com } ``` -------------------------------- ### Get Country Information in Go Source: https://context7.com/biter777/countries/llms.txt Retrieves all structured information for a given country, including name, codes, capital, and currency. It supports direct field access and JSON serialization. Dependencies include the 'encoding/json' and 'github.com/biter777/countries' packages. ```go package main import ( "fmt" "encoding/json" "github.com/biter777/countries" ) func main() { // Get complete country information info := countries.Japan.Info() // Access all fields directly fmt.Printf("Name: %v\n", info.Name) // Japan fmt.Printf("Code: %d\n", info.Code) // 392 fmt.Printf("Alpha-2: %v\n", info.Alpha2) // JP fmt.Printf("Alpha-3: %v\n", info.Alpha3) // JPN fmt.Printf("IOC: %v\n", info.IOC) // JPN fmt.Printf("FIFA: %v\n", info.FIFA) // JPN fmt.Printf("FIPS: %v\n", info.FIPS) // JA fmt.Printf("Capital: %v\n", info.Capital) // Tokyo fmt.Printf("Domain: %v\n", info.Domain) // .jp fmt.Printf("Region: %v\n", info.Region) // Asia fmt.Printf("Emoji: %v\n", info.Emoji) // 🇯🇵 fmt.Printf("Currency: %v\n", info.Currency) // Yen // JSON serialization ready jsonData, _ := json.MarshalIndent(info, "", " ") fmt.Printf("JSON:\n%s\n", jsonData) // Access currency details fmt.Printf("Currency Alpha: %v\n", info.Currency.Alpha()) // JPY fmt.Printf("Currency code: %d\n", info.Currency) // 392 } ``` -------------------------------- ### Access Country Subdivisions (ISO 3166-2) Source: https://context7.com/biter777/countries/llms.txt This snippet illustrates how to retrieve country subdivisions, such as states, provinces, and regions, using the countries library. It covers getting all subdivisions for a country, accessing detailed information for a specific subdivision (name, code, country, type), and fetching subdivisions for a country like the USA. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Get subdivisions from country japan := countries.Japan subdivisions := japan.Subdivisions() fmt.Printf("Total: %d\n", len(subdivisions)) fmt.Printf("First: %v\n", subdivisions[0]) // Hokkaido fmt.Printf("Last: %v\n", subdivisions[len(subdivisions)-1]) // Okinawa // Get detailed subdivision info subdivision := subdivisions[0] subInfo := subdivision.Info() fmt.Printf("Name: %v\n", subInfo.Name) // Hokkaido fmt.Printf("Code: %v\n", subInfo.Code) // JP-01 fmt.Printf("Country: %v\n", subInfo.Country) // Japan fmt.Printf("Type: %v\n", subInfo.SubdivisionType) // Prefecture // USA subdivisions (states) usa := countries.USA usStates := usa.Subdivisions() fmt.Printf("US subdivisions: %d\n", len(usStates)) // 50+ states and territories } ``` -------------------------------- ### Database Integration with Country and Currency Codes in Go Source: https://context7.com/biter777/countries/llms.txt Demonstrates how to use the countries library with GORM for database operations. It shows defining models with country and currency fields, connecting to a PostgreSQL database, and performing CRUD operations. This integration allows for storing and querying country and currency data efficiently. ```go package main import ( "fmt" "github.com/biter777/countries" "gorm.io/gorm" "gorm.io/driver/postgres" ) // User model with country and currency fields type User struct { gorm.Model Name string Country countries.CountryCode Currency countries.CurrencyCode } // Product model with multi-country support type Product struct { gorm.Model Name string Price float64 Currency countries.CurrencyCode ShipsTo []countries.CountryCode `gorm:"type:jsonb"` Origin countries.CountryCode } func main() { // Connect to database dsn := "host=localhost port=5432 user=myuser password=mypass dbname=mydb" db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { panic(err) } // Auto-migrate schema db.AutoMigrate(&User{}, &Product{}) // Create user with country and currency user := &User{ Name: "Helen", Country: countries.Slovenia, Currency: countries.CurrencyEUR, } result := db.Create(user) if result.Error != nil { panic(result.Error) } fmt.Printf("Created user with ID: %d\n", user.ID) // Query users by country var users []User db.Where("country = ?", countries.France).Find(&users) fmt.Printf("Found %d users from France\n", len(users)) // Create product product := &Product{ Name: "Widget", Price: 99.99, Currency: countries.CurrencyUSD, ShipsTo: []countries.CountryCode{countries.USA, countries.Canada, countries.Mexico}, Origin: countries.China, } db.Create(product) // Query and use country data var foundUser User db.First(&foundUser, user.ID) fmt.Printf("User: %s, Country: %v, Currency: %v\n", foundUser.Name, foundUser.Country, foundUser.Currency.Alpha()) } ``` -------------------------------- ### Perform Currency Operations in Go Source: https://context7.com/biter777/countries/llms.txt Provides functionality to access and query ISO 4217 currency information. It allows fetching currency details from a country, looking up currencies by name, and accessing predefined currency constants. Dependencies include the 'github.com/biter777/countries' package. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Get currency from country japan := countries.Japan currency := japan.Currency() fmt.Printf("Currency name: %v\n", currency) // Yen fmt.Printf("ISO-4217 code: %d\n", currency) // 392 fmt.Printf("Alpha code: %v\n", currency.Alpha()) // JPY fmt.Printf("Emoji: %v\n", currency.Emoji()) // 💴 // Get countries using this currency countries := currency.Countries() fmt.Printf("Used in: %v\n", countries) // [Japan] // Lookup currency by name eur := countries.CurrencyCodeByName("euro") fmt.Printf("EUR name: %v\n", eur) // Euro fmt.Printf("EUR alpha: %v\n", eur.Alpha()) // EUR fmt.Printf("EUR emoji: %v\n", eur.Emoji()) // 💶 // USD example usd := countries.CurrencyUSD fmt.Printf("USD emoji: %v\n", usd.Emoji()) // 💵 // GBP example gbp := countries.CurrencyGBP fmt.Printf("GBP emoji: %v\n", gbp.Emoji()) // 💷 } ``` -------------------------------- ### Work with Region Information (UN M.49) in Go Source: https://context7.com/biter777/countries/llms.txt Enables working with UN M.49 region codes and names. This includes retrieving region details from a country, looking up regions by name (case-insensitively), and accessing all defined regions. Dependencies include the 'github.com/biter777/countries' package. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Get region from country japan := countries.Japan region := japan.Region() fmt.Printf("Region name: %v\n", region) // Asia fmt.Printf("Region code: %d\n", region) // 142 fmt.Printf("Region (Russian): %v\n", region.StringRus()) // Азия fmt.Printf("Valid: %v\n", region.IsValid()) // true // Lookup region by name region = countries.RegionCodeByName("Europe") fmt.Printf("Region: %v\n", region) // Europe fmt.Printf("Code: %d\n", region) // 150 // Case-insensitive lookup region = countries.RegionCodeByName("EU") fmt.Printf("Region: %v\n", region) // Europe // Get all regions allRegions := countries.AllRegions() fmt.Printf("Total regions: %d\n", len(allRegions)) // 7 // Get detailed region info regionInfo := region.Info() fmt.Printf("Info name: %v\n", regionInfo.Name) // Europe fmt.Printf("Info code: %d\n", regionInfo.Code) // 150 } ``` -------------------------------- ### Direct Country Access and Properties Source: https://context7.com/biter777/countries/llms.txt Provides direct access to predefined country constants, allowing retrieval of all associated properties like names (English/Russian), ISO codes, geographic data, communication codes, and subdivisions. ```APIDOC ## Direct Country Access and Properties ### Description Access predefined country constants with all associated properties. ### Method Direct constant access (e.g., `countries.Japan`) ### Endpoint N/A ### Parameters None ### Request Example ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { japan := countries.Japan fmt.Printf("Name (English): %v\n", japan) // Japan fmt.Printf("Name (Russian): %v\n", japan.StringRus()) // Япония fmt.Printf("Alpha-2 code: %v\n", japan.Alpha2()) // JP fmt.Printf("Capital: %v\n", japan.Capital()) // Tokyo fmt.Printf("Flag emoji: %v\n", japan.Emoji()) // 🇯🇵 } ``` ### Response #### Success Response (Country Properties) - **Country Object Properties** (various types) - Direct access to properties like `Name()`, `StringRus()`, `Alpha2()`, `Alpha3()`, `Numeric()`, `IOC()`, `FIFA()`, `FIPS()`, `Capital()`, `Region()`, `CallCodes()`, `Domain()`, `Emoji()`, `Subdivisions()`. #### Response Example ```json { "Name": "Japan", "NameRus": "Япония", "Alpha2": "JP", "Alpha3": "JPN", "Numeric": 392, "Capital": "Tokyo", "Region": "Asia", "Emoji": "🇯🇵" } ``` ``` -------------------------------- ### Generate Emoji Flags and Currency Symbols in Go Source: https://context7.com/biter777/countries/llms.txt This Go code snippet demonstrates how to generate emoji representations for countries and currencies using the countries library. It covers generating flag emojis from Alpha-2 codes, 3-letter emojis from Alpha-3 codes, and currency symbols for major currencies, falling back to alpha codes for others. This is useful for visually representing geographical and monetary data. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Country flag emojis (Alpha-2 based) japan := countries.Japan fmt.Printf("Japan flag: %v\n", japan.Emoji()) // 🇯🇵 usa := countries.USA fmt.Printf("USA flag: %v\n", usa.Emoji()) // 🇺🇸 france := countries.France fmt.Printf("France flag: %v\n", france.Emoji()) // 🇫🇷 // 3-letter emoji (Alpha-3 based) fmt.Printf("Japan 3-letter: %v\n", japan.Emoji3()) // 🇯🇵🇳 // Currency emojis (major currencies only) usd := countries.CurrencyUSD fmt.Printf("USD: %v\n", usd.Emoji()) // 💵 eur := countries.CurrencyEUR fmt.Printf("EUR: %v\n", eur.Emoji()) // 💶 jpy := countries.CurrencyJPY fmt.Printf("JPY: %v\n", jpy.Emoji()) // 💴 gbp := countries.CurrencyGBP fmt.Printf("GBP: %v\n", gbp.Emoji()) // 💷 // Other currencies return alpha code cad := countries.CurrencyCAD fmt.Printf("CAD: %v\n", cad.Emoji()) // CAD } ``` -------------------------------- ### Access Direct Country Properties in Go Source: https://context7.com/biter777/countries/llms.txt Provides direct access to predefined country constants, allowing retrieval of all associated properties such as names (English and Russian), ISO codes, IOC/NOC codes, FIFA codes, FIPS codes, capital city, region, call codes, ccTLD, emoji flag, and subdivisions. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Direct access via constant japan := countries.Japan // Basic codes fmt.Printf("Name (English): %v\n", japan) // Japan fmt.Printf("Name (Russian): %v\n", japan.StringRus()) // Япония fmt.Printf("ISO-3166 numeric: %d\n", japan) // 392 fmt.Printf("Alpha-2 code: %v\n", japan.Alpha2()) // JP fmt.Printf("Alpha-3 code: %v\n", japan.Alpha3()) // JPN // Sports and international codes fmt.Printf("IOC/NOC code: %v\n", japan.IOC()) // JPN fmt.Printf("FIFA code: %v\n", japan.FIFA()) // JPN fmt.Printf("FIPS code: %v\n", japan.FIPS()) // JA // Geographic and contact info fmt.Printf("Capital: %v\n", japan.Capital()) // Tokyo fmt.Printf("Region: %v\n", japan.Region()) // Asia fmt.Printf("Region code: %d\n", japan.Region()) // 142 // Communication codes fmt.Printf("Call codes: %v\n", japan.CallCodes()) // [+81] fmt.Printf("ccTLD domain: %v\n", japan.Domain()) // .jp // Emoji representation fmt.Printf("Flag emoji: %v\n", japan.Emoji()) // 🇯🇵 // Subdivisions (states, provinces, etc.) subdivisions := japan.Subdivisions() fmt.Printf("Total subdivisions: %d\n", len(subdivisions)) fmt.Printf("First subdivision: %v\n", subdivisions[0]) // Hokkaido } ``` -------------------------------- ### Compare and Validate Country Codes Source: https://context7.com/biter777/countries/llms.txt This code snippet demonstrates how to compare and validate country codes using the countries library. It shows comparing countries by code or numeric value, case-insensitive name comparison, validating if a country code is recognized, detecting invalid country codes, and checking if two country codes refer to the same country. ```go package main import ( "fmt" "strings" "github.com/biter777/countries" ) func main() { // Compare by code/numeric if countries.ByName("angola") == countries.AGO { fmt.Println("Match by code!") // Match by code! } // Compare by name (case-insensitive) if strings.EqualFold("angola", countries.AGO.String()) { fmt.Println("Match by name!") // Match by name! } // Validate country code country := countries.ByName("Japan") if country.IsValid() { fmt.Printf("%v is valid\n", country) // Japan is valid } // Invalid country invalid := countries.ByName("Not A Country") if !invalid.IsValid() { fmt.Println("Invalid country detected") // Invalid country detected } // Direct comparison if countries.Japan == countries.JPN { fmt.Println("Same country") // Same country } } ``` -------------------------------- ### Lookup Countries by Name in Go Source: https://context7.com/biter777/countries/llms.txt Performs case-insensitive country lookups using various name formats and ISO codes. It supports full names, aliases, and ISO Alpha-2/Alpha-3 codes, returning a country object with its associated data. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Lookup by full country name country := countries.ByName("angola") fmt.Printf("Name: %v\n", country) // Angola fmt.Printf("ISO-3166 digit: %d\n", country) // 24 fmt.Printf("Alpha-2: %v\n", country.Alpha2()) // AO fmt.Printf("Alpha-3: %v\n", country.Alpha3()) // AGO // Lookup by ISO Alpha-2 code country = countries.ByName("JP") fmt.Printf("Name: %v\n", country) // Japan // Lookup by ISO Alpha-3 code country = countries.ByName("JPN") fmt.Printf("Name: %v\n", country) // Japan // Case-insensitive with various formats country = countries.ByName("UNITED KINGDOM") fmt.Printf("Name: %v\n", country) // United Kingdom // Alternative names supported country = countries.ByName("Iran, Islamic Republic Of") fmt.Printf("Alpha-3: %v\n", country.Alpha3()) // IRN } ``` -------------------------------- ### Country Lookup by Name Source: https://context7.com/biter777/countries/llms.txt Allows case-insensitive lookup of countries by their full name, aliases, or ISO codes (Alpha-2, Alpha-3). It supports various name formats and alternative country names. ```APIDOC ## Country Lookup by Name ### Description Case-insensitive country lookup supporting multiple name formats, aliases, and ISO codes. ### Method `GET` (conceptual, as this is a library function) ### Endpoint `countries.ByName(name string)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { country := countries.ByName("angola") fmt.Printf("Name: %v\n", country) // Angola country = countries.ByName("JP") fmt.Printf("Name: %v\n", country) // Japan country = countries.ByName("JPN") fmt.Printf("Name: %v\n", country) // Japan } ``` ### Response #### Success Response (Country Object) - **Country Object** (countries.Country) - Represents the found country with its properties. #### Response Example ```json { "Name": "Angola", "Alpha2": "AO", "Alpha3": "AGO", "Numeric": 24 } ``` ``` -------------------------------- ### Access Calling Codes (ITU-T E.164) in Go Source: https://context7.com/biter777/countries/llms.txt Facilitates access to international dialing codes for countries based on ITU-T E.164. It allows fetching calling codes for a specific country, retrieving details for a given code, and listing all available calling codes. Dependencies include the 'github.com/biter777/countries' package. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Get calling codes for a country japan := countries.Japan callCodes := japan.CallCodes() fmt.Printf("Call codes: %v\n", callCodes) // [+81] fmt.Printf("First code: %v\n", callCodes[0]) // +81 // USA has multiple calling codes usa := countries.USA usCodes := usa.CallCodes() fmt.Printf("US codes: %v\n", usCodes) // [+1] // Get detailed call code info callCode := countries.CallCode1 info := callCode.Info() fmt.Printf("Code: %v\n", info.Code) // +1 fmt.Printf("Countries: %v\n", info.Countries) // Multiple countries // All calling codes allCodes := countries.AllCallCodes() fmt.Printf("Total codes: %d\n", len(allCodes)) // 264 } ``` -------------------------------- ### Country Lookup by Numeric Code Source: https://context7.com/biter777/countries/llms.txt Retrieves country information using the ISO 3166-1 numeric code. Handles invalid codes by returning an 'Unknown' country object. ```APIDOC ## Country Lookup by Numeric Code ### Description Lookup countries using ISO 3166-1 numeric codes. ### Method `GET` (conceptual, as this is a library function) ### Endpoint `countries.ByNumeric(code int)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { country := countries.ByNumeric(392) fmt.Printf("Name: %v\n", country) // Japan country = countries.ByNumeric(99999) fmt.Printf("Valid: %v\n", country.IsValid()) // false } ``` ### Response #### Success Response (Country Object) - **Country Object** (countries.Country) - Represents the found country with its properties, or an unknown country if the code is invalid. #### Response Example ```json { "Name": "Japan", "Alpha2": "JP", "Alpha3": "JPN", "Numeric": 392 } ``` ``` -------------------------------- ### Lookup Countries by Numeric Code in Go Source: https://context7.com/biter777/countries/llms.txt Retrieves country data using its ISO 3166-1 numeric code. This function returns a country object, or an 'Unknown' country if the code is invalid. ```go package main import ( "fmt" "github.com/biter777/countries" ) func main() { // Lookup by ISO 3166-1 numeric code country := countries.ByNumeric(392) fmt.Printf("Name: %v\n", country) // Japan fmt.Printf("Alpha-2: %v\n", country.Alpha2()) // JP fmt.Printf("Alpha-3: %v\n", country.Alpha3()) // JPN // Lookup Austria country = countries.ByNumeric(40) fmt.Printf("Name: %v\n", country) // Austria fmt.Printf("ISO code: %d\n", country) // 40 // Invalid code returns Unknown country = countries.ByNumeric(99999) fmt.Printf("Valid: %v\n", country.IsValid()) // false } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.