有人摆弄 https://en.wikipedia.org/wiki/Regional_Indicator_Symbol ?我想知道如何将美国印成国旗🇺🇸
package main
import (
"html/template"
"os"
)
func main() {
t, err := template.New("").Parse(`
<p>{{ .Country }}</p>
<p>Want 🇺🇸</p>
`)
if err != nil {
panic(err)
}
err = t.Execute(os.Stdout, map[string]interface{}{
"Country": "US",
})
if err != nil {
panic(err)
}
}
https://play.golang.org/p/UErXxL645rV
IIUC 我需要将 0x1F1A5 添加到大写拉丁字母,但我不知道如何在 Go 中执行此操作。
更新:Eric Hill 建议:https://play.golang.org/p/hEsScaZSh1I .. 谁能提出改进意见?
最佳答案
我建议的唯一改进是添加输入字符是一对 ASCII 大写字母的验证。这是模板函数的示例:
func(x string) (string, error) {
if len(x) != 2 {
return "", errors.New("country code must be two letters")
}
if x[0] < 'A' || x[0] > 'Z' || x[1] < 'A' || x[1] > 'Z' {
return "", errors.New("invalid country code")
}
return string('🇦'+rune(x[0])-'A') + string('🇦'+rune(x[1])-'A'), nil
}
关于go - 如何将两个字母的国家代码映射到表情符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48887397/