• golang执行命令实时输出(协程通过channel更新数据到主进程)


    type Charset string

    const (
    UTF8 = Charset("UTF-8")
    GB18030 = Charset("GB18030")
    )
    //对字符进行转码
    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
    }

    func TestCmdOut(t *testing.T) { fmt.Println("start") cmdstr := "dir /s C:" cmd := exec.Command("cmd","/c",cmdstr) fmt.Println("11111111") stdout,err := cmd.StdoutPipe() if err != nil { fmt.Println(err) return } stderr,err := cmd.StderrPipe() if err != nil { fmt.Println(err) return } err = cmd.Start() if err != nil { fmt.Println(err) } stdoutScanner := bufio.NewScanner(stdout) stderrScanner := bufio.NewScanner(stderr) charset := GB18030 totalOut := "" totalErr := "" outChan := make(chan string, 10000) errChan := make(chan string, 10000) exeEnd := false go func(scanner *bufio.Scanner) { for scanner.Scan() { stdoutstr := ConvertByte2String(scanner.Bytes(), charset) outChan <- stdoutstr } exeEnd = true }(stdoutScanner) go func(scanner *bufio.Scanner) { for scanner.Scan() { stderrstr := ConvertByte2String(scanner.Bytes(), charset) errChan <- stderrstr } }(stderrScanner) j := 0 for { for i:=0;i<10000;i++ { select { case outTemp := <- outChan: fmt.Println("chan:"+outTemp) totalOut += " " + outTemp default: fmt.Println(fmt.Sprintf("j:%d",j)) fmt.Println("direct end") goto outexit } } outexit: for i:=0;i<10000;i++ { select { case errTemp := <- errChan: totalErr += " " + errTemp default: fmt.Println("err direct end") goto errexit } } errexit: fmt.Println(fmt.Sprintf("totalOut:%s",totalOut)) fmt.Println(fmt.Sprintf("totalErr:%s",totalErr)) time.Sleep(1*time.Second) fmt.Println(exeEnd) if exeEnd { j += 1 } if j>=2 { break } } err = cmd.Wait() }

      

  • 相关阅读:
    Vue组件以及组件之间的通信
    VueRouter和Vue生命周期(钩子函数)
    Vuex、axios以及跨域请求处理
    element-ui和npm、webpack、vue-cli搭建Vue项目
    2018PyCharm激活方法
    pycharm修改选中字体颜色
    为自己的博客园添加目录锚点和返回顶部
    python初识
    JAVA判断当前日期是节假日还是工作日
    springmvc使用freemarker
  • 原文地址:https://www.cnblogs.com/zipon/p/13915541.html
Copyright © 2020-2023  润新知