Go Windows 执行CMD出现中文乱码的解决方法

180it 2020-10-26 PM 493℃ 0条
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
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

Go Windows 执行CMD出现中文乱码的解决方法