package main
import (
"fmt"
"io"
"os/exec"
)
func main() {
fmt.Println("vim-go")
// pipeReader, pipeWriter := io.Pipe()
binPath := "../03-ncpk-nvm/nvm"
binArgs := []string{
"-debug",
"true",
//"--file",
//"./resource/script000-debug.nsmb",
//"--contrl",
//"./resource/000-uc-control.json",
//"--param",
//"./resource/000-uc-param.json",
}
// 进程绑定输入和输出到管道
process := exec.Command(binPath, binArgs...)
//process.Stdout = pipeWriter
//process.Stdin = pipeReader
stdin, err := process.StdinPipe()
if err != nil {
panic(err)
}
stdout, err := process.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := process.StderrPipe()
if err != nil {
panic(err)
}
chQuit := make(chan int)
go func(ch chan int) {
for {
bytBuf := make([]byte, 1024)
n, err_read := stderr.Read(bytBuf)
if err_read != nil {
if err_read == io.EOF {
fmt.Printf("[stderr] eof, n=%v\n", n)
ch <- 1
return
} else {
panic(err_read)
}
}
fmt.Printf("[stderr] n=%v, recv:\n%v\n", n, string(bytBuf))
}
}(chQuit)
go func() {
for {
bytBuf := make([]byte, 1024)
fmt.Println("[stdout] wait")
n, err_read := stdout.Read(bytBuf)
if err_read != nil {
panic(err_read)
}
fmt.Printf("[stdout] n=%v, recv:\n%v\n", n, string(bytBuf))
}
}()
err = process.Start()
if err != nil {
panic(err)
}
for {
var input string
fmt.Scan(&input)
sw := input + "\n"
bytWrite := []byte(sw)
nw, err_write := stdin.Write(bytWrite)
if err_write != nil {
panic(err_write)
}
fmt.Printf("[stdin] n=%v, send: %v\n", nw, sw)
// time.Sleep(10 * time.Millisecond)
}
<-chQuit
select {}
}
标签:pipe,封装,err,nil,process,fmt,golang,read,io
From: https://www.cnblogs.com/jiftle/p/17904999.html