41 lines
718 B
Go
41 lines
718 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Printf("Hello\n")
|
|
|
|
cmd := exec.Command("sh", "-c", "echo Wout; sleep 1; echo 1>&2 Werr; sleep 1; echo Wout2; sleep 1; echo 1>&2 Werr2")
|
|
stdout, _ := cmd.StdoutPipe()
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
cout := make(chan int)
|
|
go func() {
|
|
scanout := bufio.NewScanner(stdout)
|
|
for scanout.Scan() {
|
|
text := strings.TrimSpace(scanout.Text())
|
|
fmt.Printf("[stdout] %s\n", text)
|
|
}
|
|
cout <- 1
|
|
}()
|
|
|
|
cerr := make(chan int)
|
|
go func() {
|
|
scanerr := bufio.NewScanner(stderr)
|
|
for scanerr.Scan() {
|
|
text := strings.TrimSpace(scanerr.Text())
|
|
fmt.Printf("[stderr] %s\n", text)
|
|
}
|
|
cerr <- 1
|
|
}()
|
|
|
|
cmd.Start()
|
|
<-cout
|
|
<-cerr
|
|
}
|