go - 如何将两个字母的国家代码映射到表情符号?

标签 go unicode emoji

有人摆弄 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
}

https://play.golang.org/p/Nkg23qsr2Ga

关于go - 如何将两个字母的国家代码映射到表情符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48887397/

相关文章:

python - python 中的字符和字节

iphone - 我可以在 iOS 项目名称中添加表情符号图标吗?

string - 在golang中,为什么 `a := []int32("hello")` work but not ` a := []int ("hello")`?

macos - 在 Xquartz xterm 中显示 unicode 字体

go - 请解释 golang 类型是否按值传递

Unicode 字符串上的 Python、len 和切片

swift - 将 unicode 转换为表情符号

swift - NSAttributedString 和表情符号 : the range I never set seems to be set

go - 如何在 computeService.Zones.List(project) Google Cloud Platform API 中添加过滤器

go module @latest 找到但不包含包