'how to execute and get the result from interactive command in go?
func main() {
//r, w := io.Pipe()
//r1, w1 := io.Pipe()
cmd := exec.Command("tr", "a-z", "A-Z")
in, _ := cmd.StdinPipe()
out, _ := cmd.StdoutPipe()
go func() {
fmt.Fprintf(in, "bbbb\n")
fmt.Fprintf(in, "ccccc\n")
}()
go func() {
scanner := bufio.NewScanner(out)
for scanner.Scan() {
fmt.Printf("data is %q \n", scanner.Text()) //it print nothing
}
}()
cmd.Start()
cmd.Wait()
}
here is my code, I want to run an interactive command and get the result. and not to stop the command process, I wish it can always run.
I try set Stdout to system, I will print the result. but I can't get it in code.
cmd.Stdout = os.Stdout
I try close output, but the application stop, but I hope it can always run.
go func() {
fmt.Fprintf(in, "bbbb\n")
fmt.Fprintf(in, "ccccc\n")
in.Close()
}()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
