如何在Golang中使用os / exec处理用户输入? 我无法停止输入阶段

First, I build a command as exec.exe from this:

package main
import "fmt"
func main() {
    var input string
    fmt.Println("input a value")
    fmt.Scanln(&input)
    fmt.Println(input)

    fmt.Println("input another value")
    fmt.Scanln(&input)
    fmt.Println(input)
}

Then I want to use os/exec pacage to run it:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("G:\\go_workspace\\GOPATH\\src\\pjx\\modules\\exec\\exec")

    stdin, e := cmd.StdinPipe()
    if e != nil {
        panic(e)
    }
    stdout, e := cmd.StdoutPipe()
    if e != nil {
        panic(e)
    }
    if e:=cmd.Start();e!=nil {
        panic(e)
    }
    stdin.Write([]byte("hello"))
    var buf = make([]byte, 512)
    n, e := stdout.Read(buf)
    if e != nil {
        panic(e)
    }
    fmt.Println(string(buf[:n]))

    if e := cmd.Wait(); e != nil {
        panic(e)
    }
}

Finally I run it, and result will pause on the user input stage,like:

(in case picture is not loaded, they're paused on input stage)

please input a value:

1
12
232

unexpected result, pausing on user input stage

Did I use cmd pipe in a wrong way?

The program is blocking because the fmt.Scanln in the subprocess is waiting for the character (an EOF would also cause it to return). To avoid blocking, your input should include two s, or you can just call 'stdin.Close()' to indicate that the input stream is done.

And since the subprocess calls Scanln and Println multiple times, a single call to stdout.Read may not read the complete output from the subprocess. You can keep calling the stdout.Read() until an io.EOF error is returned, or just use the ioutil.ReadAll.

func main() {
    cmd := exec.Command("G:\\go_workspace\\GOPATH\\src\\pjx\\modules\\exec\\exec")

    stdin, e := cmd.StdinPipe()
    if e != nil {
        panic(e)
    }

    stdout, e := cmd.StdoutPipe()
    if e != nil {
        panic(e)
    }
    if e := cmd.Start(); e != nil {
        panic(e)
    }
    _, e = stdin.Write([]byte("hello
world
"))
    if e != nil {
        panic(e)
    }
    stdin.Close()

    out, _ := ioutil.ReadAll(stdout)
    // or you can use a loop
    //for {
    //  var buf = make([]byte, 512)
    //  n, e := stdout.Read(buf)
    //  if e == io.EOF {
    //      break
    //  }
    //  if e != nil {
    //      panic(e)
    //  }
    //  fmt.Println(string(buf[:n]))
    //}

    fmt.Println(string(out))

    if e := cmd.Wait(); e != nil {
        panic(e)
    }
}

You need to be listening to the output of the executing program. When you write "hello", the program is probably still writing to its stdout. Try this:

    go func() {
        in := bufio.NewReader(stdout)
        for {
            s, err := in.ReadString('
')
            if err != nil {
                return
            }
            fmt.Println(s)
        }
    }()

    if e := cmd.Start(); e != nil {
        panic(e)
    }

    stdin.Write([]byte("hello
"))
    stdin.Write([]byte("hello2
"))
    if e := cmd.Wait(); e != nil {
        panic(e)
    }

One way to do it

cmd := exec.Command("...") // Change to your path
stdin, err := cmd.StdinPipe()
if err != nil {
    panic(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
    panic(err)
}

buf := bytes.NewBuffer(nil)
// read the stdout continuously in a separate goroutine and capture it in buf
go func() {
    io.Copy(buf, stdout)
}()

if err := cmd.Start(); err != nil {
    panic(err)
}

stdin.Write([]byte("hello
")) // Send 
 to submit
stdin.Write([]byte("world
"))

if err := cmd.Wait(); err != nil {
    panic(err)
}

fmt.Fprint(os.Stdout, buf)

Result:

➜ go run main.go
input a value
hello
input another value
world