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.
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 thegolang tutorialcolumn!
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!