Windows下在CMD下执行Go出现中文乱码的解决方法
作者:rznice 时间:2024-04-25 15:17:27
在cmd下运行go程序或者是GOLAND的Terminal下运行go程序会出现中文乱码的情况。
go run ttypemain.go
���� Ping [127.0.0.1] ���� 32 �ֽڵ�����:
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128127.0.0.1 �� Ping ͳ����Ϣ:
���ݰ�: �ѷ��� = 4���ѽ��� = 4����ʧ = 0 (0% ��ʧ)��
�����г̵Ĺ���ʱ��(�Ժ���Ϊ��λ):
因为Go的编码是 UTF-8,而CMD的活动页是cp936(GBK),因此产生乱码。
在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容。在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即编码是中文字符集或者英文字符集。
在CMD或者Terminal下运行chcp查看活动页代码:
chcp
活动代码页: 936
得到的结果是 中文 936,UTF-8的代码页为65001,可以直接使用 chcp 65001 将活动代码页 改成65001,这样UTF-8编码的就显示正常了。
chcp 65001
Active code page: 65001
go run ttypemain.go
Pinging [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
或者将中文转成UTF-8的编码,完整代码如下:
package main
import (
"bufio"
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
"os/exec"
)
type Charset string
const (
UTF8 = Charset("UTF-8")
GB18030 = Charset("GB18030")
)
func main() {
command := "ping"
params := []string{"127.0.0.1","-t"}
cmd := exec.Command(command, params...)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
return
}
cmd.Start()
in := bufio.NewScanner(stdout)
for in.Scan() {
cmdRe:=ConvertByte2String(in.Bytes(),"GB18030")
fmt.Println(cmdRe)
}
cmd.Wait()
}
func ConvertByte2String(byte []byte, charset Charset) string {
var str string
switch charset {
case GB18030:
var decodeBytes,_=simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
str= string(decodeBytes)
case UTF8:
fallthrough
default:
str = string(byte)
}
return str
}
正在 Ping 127.0.0.1 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来源:https://blog.csdn.net/rznice/article/details/88122923
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PS笔刷,样式,形状、渐变、滤镜载入方式及使用
![](https://img.aspxhome.com/file/UploadPic/200710/17/2007101711564849s.gif)
网页广告 Banner 设计图文手册
![](https://img.aspxhome.com/file/UploadPic/up/2007101820293575.jpg)
JavaScript详解使用Promise处理回调地狱与async await修饰符
selenium+headless chrome爬虫的实现示例
TypeScript函数和类型断言实例详解
![](https://img.aspxhome.com/file/2023/1/136431_0s.jpg)
ajax实现Dig程序中的投票
![](https://img.aspxhome.com/file/UploadPic/20081/22/2008122173320740s.jpg)
详解python metaclass(元类)
Python 虚拟机字典dict内存优化方法解析
![](https://img.aspxhome.com/file/2023/2/93562_0s.png)
python GUI库图形界面开发之PyQt5多行文本框控件QTextEdit详细使用方法实例
![](https://img.aspxhome.com/file/2023/6/117546_0s.png)
浏览器是怎样工作的(一):基础知识
vue相关配置文件详解及多环境配置详细步骤
![](https://img.aspxhome.com/file/2023/5/139755_0s.jpg)
使用Spring Boot实现操作数据库的接口的过程
![](https://img.aspxhome.com/file/2023/3/84773_0s.jpg)
基于python判断字符串括号是否闭合{}[]()
浅谈Vue使用Cascader级联选择器数据回显中的坑
分享10个Js的小型库,效果真的很棒
SQL Server日期计算第1/2页
MAC下Anaconda+Pyspark安装配置详细步骤
![](https://img.aspxhome.com/file/2023/4/88934_0s.png)
iscroll动态加载数据完美解决方法
Matplotlib绘制子图的常见几种方法
![](https://img.aspxhome.com/file/2023/8/102338_0s.png)