Country code to flag emojie in Go
Friday 4 November 2022

I’m in the process of rewriting my news website web interface in #go.

The interface is written in Ruby/Rails right now and I’m looking for improving my Go writing skills so I decided to rewrite this part in Go.

I have a function in my code that convert the country code like “eg” to it’s flag emojie (you can see that at the bottom of the website or in the sidebar).

This code looks like so:

1def flag(code)
2  code.upcase.tr('A-Z', "\u{1F1E6}-\u{1F1FF}")
3end

After translating it to Go it looks like this:

 1import (
 2	"golang.org/x/text/runes"
 3	"golang.org/x/text/transform"
 4)
 5
 6var FlagReplacer = runes.Map(func(r rune) rune {
 7	return r - 'a' + '\U0001F1E6'
 8})
 9
10func Flag(code string) string {
11	s, _, _ := transform.String(FlagReplacer, code)
12
13	return s
14}

this will return the flag emojie that correspond to the country code. for example “eg” will return “🇪🇬”

See Also