Home  >  Article  >  Backend Development  >  How to solve the garbled problem of go language

How to solve the garbled problem of go language

藏色散人
藏色散人Original
2020-12-18 11:53:033146browse

The solution to the garbled go language: first download the third-party software package; then unzip it and change the folder to text; then create a folder under src of the go installation path and put the text folder In it; finally complete the encoding conversion.

How to solve the garbled problem of go language

Environment of this article: Windows 7 system, Go1.11.2 version, this article is applicable to all brands of computers.

Recommended tutorial: "go language tutorial"

go language Chinese garbled solution

windows When I was learning Golang to make a crawler, the problem of Chinese garbled characters appeared. There are not many introductions to this aspect on the Internet, so I will make a record of the solution here.

Combined with several blogs, here is the clearest solution:

1. Download the third-party software package first: https://github.com/golang/text

2. Then unzip it and change the folder to text

3. Create a folder under src of the go installation path. The directory is roughly: C:\Go\src\golang.org\x\, Then place the text folder in step 2 in this directory, which is: C:\Go\src\golang.org\x\text;

4. Now the encoding conversion can be completed;

Usage examples are as follows: (refer to teacher ccmouse’s code)

package main
// gopm get -g -v golang.org/x/text
import (
"net/http"
"fmt"
"io/ioutil"
"golang.org/x/text/encoding/simplifiedchinese"
// "golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
func main()  {
fmt.Println("hello world")
resp, err := http.Get("http://city.zhenai.com/xian")
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("Error: status code", resp.StatusCode)
return
}
utf8Reader := transform.NewReader(resp.Body, 
simplifiedchinese.GBK.NewDecoder())
all, err := ioutil.ReadAll(utf8Reader)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", all)
}

For more related technical articles, please visit the golang tutorial column!

The above is the detailed content of How to solve the garbled problem of go language. 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