This is an example of using cmd.ExtraFiles
in golang with a os.Pipe()
There are 2 things that might be a bit tricky:
- the order of closing the read and write side of the pipe. I got this right by reading the implementation of cmd.StderrPipe.
- the number of the file descriptor for the child. I'm not sure of a good way to cleanly determine what that is. Golang cmd processes do not inherit all open filehandles, so you can't just use pipeWrite.Fd().
Ultimately, we did this because we were working with tssnvread which is possibly the worst user interface I've ever seen. We wanted to read the contents without writing to a file on the filesystem. To do so, we were able to tell it to write to /proc/self/fd/.