Skip to content

Instantly share code, notes, and snippets.

@GwynethLlewelyn
Created August 4, 2023 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GwynethLlewelyn/45092c8199f06292704286b102919112 to your computer and use it in GitHub Desktop.
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
// 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)
}
@GwynethLlewelyn
Copy link
Author

GwynethLlewelyn commented Aug 4, 2023

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 🚫)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment