Home>Article>Backend Development> Golang uses socket Chinese garbled solution

Golang uses socket Chinese garbled solution

尚
Original
2019-12-24 11:52:43 11889browse

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.

Golang uses socket Chinese garbled solution

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is map in golang a pointer? Next article:Is map in golang a pointer?