我正在用 Go 编写一个程序,它执行一个类似服务器的程序(也是 Go)。现在我希望在启动父程序的终端窗口中有子程序的 stdout。一种方法是使用 cmd.Output()
函数,但这只在进程退出后才打印标准输出。(这是一个问题,因为这个类似服务器的程序运行时间很长,我想读取日志输出)
The variable out
is of type io.ReadCloser
and I don't know what I should do with it to achieve my task, and I can't find anything helpful on the web on this topic.
func main() {
cmd := exec.Command("/path/to/my/child/program")
out, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
}
err = cmd.Start()
if err != nil {
fmt.Println(err)
}
//fmt.Println(out)
cmd.Wait()
}
Explanation to the code: uncomment the Println
function to get the code to compile, I know that Println(out io.ReadCloser)
is not a meaningful function.
(它生成输出 &{3 |0 <nil> 0}
)只需要这两行代码就可以进行编译。