【发布时间】:2021-06-30 05:28:49
【问题描述】:
我刚刚阅读了一些 Go 代码,它们执行以下操作:
type someType struct {
...
...
rpipe io.ReadCloser
wpipe io.WriteCloser
}
var inst someType
inst.rpipe, inst.wpipe, _ := os.Pipe()
cmd := exec.Command("some_binary", args...)
cmd.Stdout = inst.wpipe
cmd.Stderr = inst.wpipe
if err := cmd.Start(); err != nil {
....
}
inst.wpipe.Close()
inst.wpipe = nil
some_binary 是一个长期运行的进程。
- 为什么
inst.wpipe关闭并设置为零?如果不关闭会怎样?关闭 inst.wpipe 是否常见/必要? -
dup2(pipe_fd[1], 1)是cmd.Stdout = inst.wpipe; inst.wpipe.Close()的 C 类似物吗?
【问题讨论】:
-
哦,抱歉,打错了。让我修复它。 stdout 和 stderr 都设置为 wpipe。