如何在Golang中将符文转换为类似`\ u554a`的unicode样式字符串?

har*_*ass 20 unicode go

如果你跑fmt.Println("\u554a"),它会显示'啊'.

但是如何\u554a从符文"啊"中获取unicode-style-string ?

Dar*_*tle 16

package main

import "fmt"
import "strconv"

func main() {
    quoted := strconv.QuoteRuneToASCII('?') // quoted = "'\u554a'"
    unquoted := quoted[1:len(quoted)-1]      // unquoted = "\u554a"
    fmt.Println(unquoted)
}
Run Code Online (Sandbox Code Playgroud)

这输出:

\u554a


har*_*ass 12

恕我直言,它应该更好:

func RuneToAscii(r rune) string {
    if r < 128 {
        return string(r)
    } else {
        return "\\u" + strconv.FormatInt(int64(r), 16)
    }
}
Run Code Online (Sandbox Code Playgroud)