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

Golang uses socket Chinese garbled solution

Release: 2019-12-24 11:52:43
Original
12098 people have browsed it

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
Copy after login

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 
}
Copy after login

Use the function to convert the encoding format:

v, err := GbToUtf8(buf[0:n])
Copy after login

For more golang knowledge, please pay attention PHP Chinese website golang tutorial column.

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
How to choose golang web mvc framework
From 1970-01-01 08:00:00
0
0
0
Is it necessary to use nginx when using golang?
From 1970-01-01 08:00:00
0
0
0
golang - vim plug-in to write go
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template