Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the solution to Chinese garbled code when executing Go under CMD?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the solution to the occurrence of Chinese garbled code in Go under CMD? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. The error message is as follows

2. Cause analysis.

Because the coding of Go is UTF-8, and the active page of CMD is cp936 (GBK), garbled code is generated. In Chinese Windows systems, if a text file is UTF-8 encoded, the contents of the file will not be displayed correctly in the CMD.exe command line window (the so-called DOS window). By default, the code page used in the command line window is Chinese or American, that is, the code is a Chinese character set or an English character set.

3. Solution

Golang defaults to utf8 when dealing with Chinese, and garbled codes will occur when other characters such as GBK are encountered. The transcoding under the official golang.org/x/text/encoding/simplifiedchinese package of golang is described here.

Simplifiedchinese.GB18030.NewDecoder () .Bytes () 4, encapsulation method package mainimport "golang.org/x/text/encoding/simplifiedchinese" type Charset stringconst (UTF8 = Charset ("UTF-8") GB18030 = Charset ("GB18030") func ConvertByte2String (byte [] byte, charset Charset) string {var str string switch charset {case GB18030: decodeBytes _: = simplifiedchinese.GB18030.NewDecoder () .Bytes (byte) str = string (decodeBytes) case UTF8: fallthrough default: str = string (byte)} return str} 5 Solve garbled complete code package mainimport ("bytes"fmt"golang.org/x/text/encoding/simplifiedchinese"os"os/exec") type Charset stringconst (UTF8 = Charset ("UTF-8") GB18030 = Charset ("GB18030") func ConvertByte2String (byte [] byte Charset Charset) string {var str string switch charset {case GB18030: decodeBytes _: = simplifiedchinese.GB18030.NewDecoder () .Bytes (byte) str = string (decodeBytes) case UTF8: fallthrough default: str = string (byte)} return str} / / the full path to the bat file executed by arg / / returns the error message and message prompt func ExecCommand (arg string) (error String) {c: = exec.Command ("cmd", "/ C", arg) w: = bytes.NewBuffer (nil) c.Stderr = w message: = "execute" + arg + "File extraction data successfully" _ Err1: = os.Stat (arg) var err error / / determine whether the file exists if err1! = nil {err: = c.Run () if err! = nil {fmt.Printf ("Run returns:% s\ n" Err)} / / handle Chinese garbled garbledStr: = ConvertByte2String (w.Bytes (), GB18030) message = err1.Error () + garbledStr / / file does not exist and execute error return err Message} else {err = c.Run () if err! = nil {/ / handle Chinese garbled garbledStr: = ConvertByte2String (w.Bytes (), GB18030) / / file exists executable bat file error return err GarbledStr}} / / File exists and bat file is executed successfully return err, message} read the above content Do you know how to solve the problem of Chinese garbled code in Go under CMD? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report