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=128

127.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

标签:CMD,Go,乱码
0
投稿

猜你喜欢

  • PS笔刷,样式,形状、渐变、滤镜载入方式及使用

    2007-10-17 11:47:00
  • 网页广告 Banner 设计图文手册

    2007-10-18 19:56:00
  • JavaScript详解使用Promise处理回调地狱与async await修饰符

    2024-04-22 22:43:58
  • selenium+headless chrome爬虫的实现示例

    2022-01-31 19:29:50
  • TypeScript函数和类型断言实例详解

    2024-04-17 10:00:09
  • ajax实现Dig程序中的投票

    2008-01-22 17:27:00
  • 详解python metaclass(元类)

    2023-08-21 10:09:04
  • Python 虚拟机字典dict内存优化方法解析

    2022-03-04 08:20:56
  • python GUI库图形界面开发之PyQt5多行文本框控件QTextEdit详细使用方法实例

    2023-10-09 05:33:38
  • 浏览器是怎样工作的(一):基础知识

    2012-05-09 20:32:48
  • vue相关配置文件详解及多环境配置详细步骤

    2023-07-02 16:39:39
  • 使用Spring Boot实现操作数据库的接口的过程

    2024-01-25 02:02:49
  • 基于python判断字符串括号是否闭合{}[]()

    2022-03-25 15:58:45
  • 浅谈Vue使用Cascader级联选择器数据回显中的坑

    2024-04-10 13:47:27
  • 分享10个Js的小型库,效果真的很棒

    2009-08-27 15:38:00
  • SQL Server日期计算第1/2页

    2024-01-23 20:30:59
  • MAC下Anaconda+Pyspark安装配置详细步骤

    2021-02-11 18:15:51
  • iscroll动态加载数据完美解决方法

    2024-04-10 13:59:01
  • Matplotlib绘制子图的常见几种方法

    2022-04-04 06:19:47
  • php 异常处理实现代码

    2023-11-14 23:47:12
  • asp之家 网络编程 m.aspxhome.com