Home>Article>Backend Development> Golang uses socket Chinese garbled solution
When writing Socket in go, I found that when inputting Chinese from the client, the server could not display garbled characters normally.
We can solve it by transcoding Chinese.
Transcoding can be implemented using go’s official golang.org/x/text package.
The installation command is as follows:
go get golang.org/x/text
The function to convert the encoding format is as follows:
func GbToUtf8(s []byte) ([]byte, error) { //reader := transform.NewReader(byte.NewReader(s), simplifiedchinese.GBK.NewEncoder()) reader := transform.NewReader(bytes.NewReader(s),simplifiedchinese.GBK.NewDecoder()) d, e := ioutil.ReadAll(reader) if e != nil { return nil, e } return d, nil }
Use the function to convert the encoding format:
v, err := GbToUtf8(buf[0:n])
For more golang knowledge, please pay attention PHP Chinese websitegolang tutorialcolumn.
The above is the detailed content of Golang uses socket Chinese garbled solution. For more information, please follow other related articles on the PHP Chinese website!