golang cmd garbled code

WBOY
Release: 2023-05-21 14:32:37
Original
567 people have browsed it

With the increasing popularity of golang in China, more and more novice developers have begun to come into contact with this language and encountered various problems in practice. One of the common problems is the problem of garbled Chinese characters in golang cmd.

In golang, compiling and running programs through the cmd command line is a very common operation. However, you may encounter some problems during operation, such as:

  1. Chinese garbled characters are displayed on the cmd console, and the output results cannot be viewed correctly;
  2. When calling other programs, Chinese characters cannot be transmitted correctly. Parameters etc.

For these problems, we will give solutions below.

  1. Set the console encoding

First, we need to confirm the encoding method of the cmd console, which can be set by the following steps:

Open cmd Console, right-click the window title, select "Properties" -> "Options" -> "Local Options", select "Chinese (Simplified, China)" or other languages that support Chinese in the "Language" drop-down box, and then Click OK.

After confirming the settings, we need to specify the encoding method in the program to ensure the correctness of the console output.

Sample code:

package main import ( "fmt" "os" "github.com/axgle/mahonia" ) func main() { // 创建一个utf8编码的文本 s := "中文" // 定义一个gbk编码器 dec := mahonia.NewEncoder("gbk") // 将utf8编码的文本转换成gbk编码 result := dec.ConvertString(s) // 输出到控制台 fmt.Println(result) // 将gbk编码的文本写入文件 file, _ := os.Create("test.txt") defer file.Close() file.WriteString(result) }
Copy after login

In the above code, we use the mahonia library to convert utf8-encoded text (golang default encoding) to gbk encoding to ensure that the output is correct.

  1. Use gbk format to call other programs

When calling other programs, we need to pay attention to whether the encoding method of the program is consistent with the encoding method of the cmd console. For example, if we want to use os to execute a command and pass Chinese parameters, then we need to convert the parameters into gbk encoding to ensure correct transmission.

Sample code:

package main import ( "os" "github.com/axgle/mahonia" ) func main() { // 定义一个gbk编码器 dec := mahonia.NewEncoder("gbk") // 将utf8编码的文本转换成gbk编码 cmdStr := dec.ConvertString("notepad 中文.txt") // 执行命令 cmd := exec.Command("cmd.exe", "/c", cmdStr) cmd.Run() }
Copy after login

In the above code, we use the mahonia library to convert the command parameters from utf8 encoding to gbk encoding, and then call cmd.exe through os to execute the command, This ensures that Chinese parameters are passed correctly.

Summary:

Through the above two methods, we can solve the problem of Chinese garbled characters in golang cmd. In actual development, we need to choose the appropriate method to solve it according to our own needs.

At the same time, we should actively feedback this issue to the golang official, hoping that it can get a better solution in future upgrades.

The above is the detailed content of golang cmd garbled code. For more information, please follow other related articles on the PHP Chinese website!

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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!