go init
This commit is contained in:
parent
7aae56415d
commit
a3d898aa50
40
main.go
Normal file
40
main.go
Normal file
@ -0,0 +1,40 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user