How to convert a rune to unicode-style-string like `\u554a` in Golang?

后端 未结 8 1776
心在旅途
心在旅途 2021-02-05 07:47

If you run fmt.Println(\"\\u554a\"), it shows \'啊\'.

But how to get unicode-style-string \\u554a from a rune \'啊\' ?

8条回答
  •  死守一世寂寞
    2021-02-05 08:24

    package main
    
    import "fmt"
    import "strconv"
    
    func main() {
        quoted := strconv.QuoteRuneToASCII('啊') // quoted = "'\u554a'"
        unquoted := quoted[1:len(quoted)-1]      // unquoted = "\u554a"
        fmt.Println(unquoted)
    }
    

    This outputs:

    \u554a
    

提交回复
热议问题