我如何使用管道分配更多的ExraFile,使其具有多个输出流?

问题描述 投票:1回答:1

我有一条命令,我会通过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。我...

go pipe io-redirection
1个回答
1
投票

问题中的代码无法编译,因为从os.Pipe返回的os.ExtraFiles值存储在接口类型为*os.Fileio.ReadCloser的变量中。指向这些类型的值的指针不能分配给io.WriteCloser

© www.soinside.com 2019 - 2024. All rights reserved.