golang中,带有表情的字符串,通过json.Marshal()后,并不会进行unicode,所以会导致存储进数据库中的表情为乱码。
例子:
func TestOther(t *testing.T) {
str := "哈哈😝👌"
strr, _ := json.Marshal(str)
t.Log(str)
t.Log(fmt.Sprintf("%s", strr))
}
输出:

下面,我们开发了一个将字符串unicode的方法:
type EmojiUnicodeString string
func (em EmojiUnicodeString) MarshalJSON() ([]byte, error) {
encodes := utf16.Encode([]rune(em))
ret := `"`
for _, enc := range encodes {
//ascii可见字符范围0-9a-zA-Z
if (enc >= 0x30 && enc <= 0x39) || (enc >= 0x41 && enc <= 0x5A) || (enc >= 0x61 && enc <= 0x7A) {
ret = ret + string(rune(enc))
} else { //其他都使用utf16编码
if encStr := strconv.FormatUint(uint64(enc), 16); encStr != "" {
//补齐4位长度
diff := 4 - len(encStr)
for i := 0; i < diff; i++ {
encStr = "0" + encStr
}
ret = ret + `\u` + encStr
}
}
}
ret = ret + `"`
return []byte(ret), nil
}
测试:
func TestOther(t *testing.T) {
str := "哈哈😝👌"
strr, _ := json.Marshal(str)
t.Log(str)
t.Log(fmt.Sprintf("%s", strr))
str2 := EmojiUnicodeString(str)
strr2, _ := json.Marshal(str2)
t.Log(fmt.Sprintf("%s", strr2))
}
输出:

可以看到,该带有表情的字符串已经unicode话,此时就可以直接进行数据库存储了。