Emad Elsaid 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:

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

After translating it to Go it looks like this:

import (
	"golang.org/x/text/runes"
	"golang.org/x/text/transform"
)

var FlagReplacer = runes.Map(func(r rune) rune {
	return r - 'a' + '\U0001F1E6'
})

func Flag(code string) string {
	s, _, _ := transform.String(FlagReplacer, code)

	return s
}

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

See Also

Converting Ruby sinatra project to Go
Copy file
Go FileSystem with fallback
Go function logging technique
Go init function
Go slice that doesn't grow past capacity
Golang Bleve Experience
🌻 Home
Mau
⌨️ Programming
Replacing Golang Regexp matching with a premitive faster solution
Xlog