Converts a string country code to an emoji in Go.
- โ Support for ISO 3166-1 alpha-2 codes (e.g., "VN")
- โ Support for ISO 3166-1 alpha-3 codes (e.g., "VNM")
- โ Support for CIOC codes (e.g., "GER")
- โ Support for Great Britain subdivisions (England, Scotland, Wales)
- โ Fuzzy matching for typos and variations (e.g., "USA" โ "US", "GERM" โ "GER")
- โ Consistent emoji output length (no trailing spaces)
go get -u github.com/yudgnahk/go-emoji-flags
Convert country codes to flag emojis:
package main
import (
"fmt"
emoji "github.com/yudgnahk/go-emoji-flags"
)
func main() {
fmt.Println(emoji.GetFlag("VNM")) // prints ๐ป๐ณ
fmt.Println(emoji.GetFlag("VN")) // prints ๐ป๐ณ
fmt.Println(emoji.GetFlag("BOB")) // prints (empty string)
}Convert flag emojis back to country codes:
code := emoji.GetCode("๐ป๐ณ") // Returns "VN"
code := emoji.GetCode("๐ด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ") // Returns "GB-ENG"Get country names from codes or flags:
name := emoji.GetName("VN") // Returns "Vietnam"
name := emoji.GetName("VNM") // Returns "Vietnam"
name := emoji.GetName("GER") // Returns "Germany"
name := emoji.GetName("๐ป๐ณ") // Returns "Vietnam"Find flags using country names (with fuzzy matching):
flag, code := emoji.GetFlagByName("Vietnam") // Returns "๐ป๐ณ", "VN"
flag, code := emoji.GetFlagByName("United States") // Returns "๐บ๐ธ", "US"
flag, code := emoji.GetFlagByName("USA") // Returns "๐บ๐ธ", "US" (alias support)
flag, code := emoji.GetFlagByName("UK") // Returns "๐ฌ๐ง", "GB" (alias support)Use GetFlagFuzzy() to handle typos or variations in country codes:
package main
import (
"fmt"
emoji "github.com/yudgnahk/go-emoji-flags"
)
func main() {
// Exact match still works
flag, code := emoji.GetFlagFuzzy("VNM")
fmt.Printf("%s (matched: %s)\n", flag, code) // ๐ป๐ณ (matched: VNM)
// Fuzzy matching handles typos (within distance of 2)
flag, code = emoji.GetFlagFuzzy("USA")
fmt.Printf("%s (matched: %s)\n", flag, code) // ๐บ๐ธ (matched: USA)
flag, code = emoji.GetFlagFuzzy("GERM")
fmt.Printf("%s (matched: %s)\n", flag, code) // ๐ฉ๐ช (matched: GER)
flag, code = emoji.GetFlagFuzzy("GB-EN")
fmt.Printf("%s (matched: %s)\n", flag, code) // ๐ด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ (matched: GB-ENG)
// Returns empty if no close match found
flag, code = emoji.GetFlagFuzzy("ZZZZZ")
fmt.Printf("'%s' (matched: '%s')\n", flag, code) // '' (matched: '')
}The GetFlag() function returns an empty string when no match is found:
flag := emoji.GetFlag("INVALID")
if flag == "" {
log.Println("Country code not found")
}Use fuzzy matching to suggest corrections to users:
userInput := "GERM"
flag, matchedCode := emoji.GetFlagFuzzy(userInput)
if flag == "" {
fmt.Printf("No match found for: %s\n", userInput)
} else if matchedCode != userInput {
fmt.Printf("Did you mean %s? %s\n", matchedCode, flag)
} else {
fmt.Printf("Found: %s\n", flag)
}Process multiple country codes efficiently:
codes := []string{"VN", "US", "GB", "INVALID"}
for _, code := range codes {
if flag := emoji.GetFlag(code); flag != "" {
fmt.Printf("%s: %s\n", code, flag)
} else {
fmt.Printf("%s: Not found\n", code)
}
}- GetFlag(): Fast O(1) map lookup - use for known-good codes
- GetFlagFuzzy(): Slower O(n) search - use only for user input that may contain typos
- Fuzzy matching distance: Limited to 2 character edits for performance
- Caching: Consider caching fuzzy results if you process the same queries repeatedly
BenchmarkGetFlag-12 35772781 33.55 ns/op 8 B/op 1 allocs/op
BenchmarkGetFlagFuzzy-12 28222648 41.78 ns/op 8 B/op 1 allocs/op
BenchmarkGetFlagFuzzyClose-12 13245 90293 ns/op 194969 B/op 4261 allocs/op
Run go test -bench=. -benchmem to see all benchmarks
- Alpha-2: 2-letter codes (e.g., VN, US, GB)
- Alpha-3: 3-letter codes (e.g., VNM, USA, GBR)
- CIOC: Olympic codes (e.g., GER for Germany, SUI for Switzerland)
- England:
GB-ENGorENG - Scotland:
GB-SCTorSCT - Wales:
GB-WLSorWLS
Converts a country code to its emoji flag. Supports ISO 3166-1 alpha-2, alpha-3, CIOC codes, and special subdivisions.
Parameters:
countryCode- 2-letter (VN), 3-letter (VNM, GER), or special codes (GB-ENG)
Returns: Flag emoji string, or empty string if not found
Finds a flag using fuzzy matching (Levenshtein distance โค 2). Prefers shorter codes when multiple matches exist.
Parameters:
input- Country code (possibly with typos)
Returns: Flag emoji and matched code, or empty strings if no match
Converts a flag emoji to its ISO 3166-1 alpha-2 country code.
Parameters:
flag- Flag emoji (e.g., ๐ป๐ณ)
Returns: Country code (e.g., "VN"), or empty string if not recognized
Converts a country code or flag emoji to the country name.
Parameters:
input- Country code (alpha-2, alpha-3, CIOC) or flag emoji
Returns: Country name, or empty string if not found
Finds a flag by country name using exact or fuzzy matching (Levenshtein distance โค 2). Supports common aliases.
Parameters:
name- Country name or alias (e.g., "Vietnam", "USA", "UK")
Returns: Flag emoji and matched code, or empty strings if no match
The library provides access to several data maps:
CountryNames- map[string]string: ISO alpha-2 codes to country namesCountryAliases- map[string]string: Common aliases to ISO alpha-2 codesCca2CodeMap- map[string]string: ISO alpha-2 code mappingsCca3CodeMap- map[string]string: ISO alpha-3 to alpha-2 code mappingsCiocCodeMap- map[string]string: CIOC to alpha-2 code mappingsSpecialEmojiMap- map[string]string: Special subdivision codes to emoji flags
... and wikipedia for the understanding about countries code (especially ISO_3166) (https://en.wikipedia.org/wiki/ISO_3166-2:GB)
I was stuck on the countries that belongs to Great Britain, which doesn't have the same format as origin emojis (go-emoji-flag does not support these countries)