Created
August 4, 2023 10:53
-
-
Save GwynethLlewelyn/45092c8199f06292704286b102919112 to your computer and use it in GitHub Desktop.
Go lang conversion from ISO-3166-1 two-letter codes to flag emoji
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To run this on the Go Playground: https://go.dev/play/p/9na7SU0ExdD | |
package main | |
import "fmt" | |
func main() { | |
country := "PT" | |
fmt.Printf("%s — Decimal: %d %d Hex: %#[1]x %#x\n", country, rune(country[0]), rune(country[1])) | |
fmt.Printf("Difference: %d (hex: %#[1]x)\n", 0x1F1E6-0x0041) | |
fmt.Printf("First letter + difference: %d (hex: %#[1]x) Type: %[1]T\n", rune(country[0])+127397) | |
fmt.Printf("Second letter + difference: %d (hex: %#[1]x) Type: %[1]T\n", rune(country[1])+127397) | |
fmt.Println("Flag: ", getFlag(country)) | |
} | |
// ISO-3166-1 two-letter country codes to flag emoji. | |
func getFlag(countryCode string) string { | |
// 0x1F1E6 - REGIONAL INDICATOR SYMBOL LETTER A | |
// 0x1F1FF - REGIONAL INDICATOR SYMBOL LETTER Z | |
// 0x0041 — LATIN CAPITAL LETTER A | |
// 0x005A — LATIN CAPITAL LETTER Z | |
return string(rune(countryCode[0])+127397) + string(rune(countryCode[1])+127397) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exercise to the reader: check if the two-letter combination is valid (and if there are, indeed, two letters in the string thus passed) and deal with that properly (e.g by displaying a different emoji such as: 🏳️ or 🏴☠️ or simply ❌ or 🚫)