Skip to content

Instantly share code, notes, and snippets.

@tyndyll
Last active August 29, 2015 14:08
Show Gist options
  • Save tyndyll/860694a18b5f6f4a98d7 to your computer and use it in GitHub Desktop.
Save tyndyll/860694a18b5f6f4a98d7 to your computer and use it in GitHub Desktop.
Printing Bytes Passing through a Pipe
package main
import (
"bufio"
"io"
)
type LoggedPipe struct {
Stdin *io.PipeReader
Stdout *PipeCloner
}
type PipeCloner struct {
io.Writer
Pipe *io.PipeWriter
}
func NewPipeCloner(p *io.PipeWriter) *PipeCloner {
mw := io.MultiWriter(os.Stdout, p)
c := &PipeCloner{bufio.NewWriter(mw), p}
return c
}
func (p *PipeCloner) Close() error {
return p.Pipe.Close()
}
func LoggedPipe() (*io.PipeReader, *PipeCloner) {
p := new(LoggedPipe)
r, w := io.Pipe()
p.Stdin = r
p.Stdout = NewPipeCloner(w)
return p.Stdin, p.Stdout
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment