SoFunction
Updated on 2025-03-05

Use of unicode code and Chinese mutual conversion function in Golang

background

The Golang chapter of knowledge sharing is a record of various knowledge I learned when using Golang in daily life. I will sort it out and share it with everyone in the form of articles to learn together. Everyone is welcome to continue to pay attention.

The knowledge sharing series currently includes Java, Golang, Linux, Docker, etc.

Development Environment

  • System: Windows 10
  • Language: Golang
  • golang version: 1.18

content

In this section, we share the mutual conversion function of unicode code and Chinese. The following are the relevant codes for this time:

1. Chinese to unicode

    str := "This is a test trick"
    textQuoted := (str)
    textUnquoted := textQuoted[1 : len(textQuoted)-1]
    ("Convert to unicode:", textUnquoted)

2. Unicode to Chinese

func main() {
    // This is Chinese to unicode    str := "This is a test trick"
    textQuoted := (str)
    textUnquoted := textQuoted[1 : len(textQuoted)-1]
    ("Convert to unicode:", textUnquoted)
    // This is unicode to Chinese    v, _ := zhToUnicode([]byte(textUnquoted))
    ("Convert to Chinese:", string(v))
}
func zhToUnicode(raw []byte) ([]byte, error) {
    str, err := (((string(raw)), `\\u`, `\u`, -1))
    if err != nil {
        return nil, err
    }
    return []byte(str), nil
}

The above is the detailed content of using the mutual conversion function of unicode code and Chinese in Golang. For more information about the conversion of Golang unicode code in Chinese, please pay attention to my other related articles!