我有一条命令,我会通过Stdin向其发送数据,并期望有28个输出流(包括Stdout
。]]
所以我想将cmd.ExtraFiles
字段与os.Pipe
的每一个一起使用。
我写了以下内容:
os.ExtraFiles
由于类型不匹配,最后两行会产生错误:
backgroundContext, cancelCommand := context.WithCancel(context.Background()) cmd := exec.CommandContext(backgroundContext, "command", args...) cmd.ExtraFiles = make([]*io.File, 27, 0) var outputPipe io.ReadCloser var inputPipe io.WriteCloser outputPipe, inputPipe, err = os.Pipe() cmd.ExtraFiles[0] = &inputPipe cmd.ExtraFiles[1] = &outputPipe
我确定我们可以分配管道,因为例如可以使用
./main.go:876:26: cannot use &inputPipe (type *io.WriteCloser) as type *os.File in assignment ./main.go:877:26: cannot use &outputPipe (type *io.ReadCloser) as type *os.File in assignment
函数,它可以按预期工作。
我应该如何使用StdoutPipe()
来做到这一点?
我有一条命令,我将通过Stdin向其发送数据,并期望28个输出流(包括Stdout)。因此,我想将cmd.ExtraFiles字段与os.Pipe一起用于每个os.ExtraFiles。我...
问题中的代码无法编译,因为从os.Pipe返回的os.ExtraFiles
值存储在接口类型为*os.File
和io.ReadCloser
的变量中。指向这些类型的值的指针不能分配给io.WriteCloser
。