how can I get stdin to exec cmd in golang

后端 未结 4 998
故里飘歌
故里飘歌 2021-02-07 06:32

I have this code

subProcess := exec.Cmd{
    Path: execAble,
    Args: []string{
        fmt.Sprintf(\"-config=%s\", *configPath),
        fmt.Sprintf(\"-serverT         


        
4条回答
  •  走了就别回头了
    2021-02-07 06:51

    While you cannot directly do this as @AlexKey wrote earlier still you can make some workarounds. If os prevents you to pipe your own standard streams who cares all you need 2 channels and 2 goroutines

    var stdinChan chan []byte
    var stdoutChan chan []byte
    
    //when something happens in stdout of your code just pipe it to stdout chan
    stdoutChan<-somehowGotDataFromStdOut
    

    then you need two funcs as i mentioned before

    func Pipein(){
        for {
            stdinFromProg.Write(<-stdInChan)
        }
    }
    

    The same idea for the stdout

提交回复
热议问题