Last active
August 29, 2015 14:08
-
-
Save tyndyll/860694a18b5f6f4a98d7 to your computer and use it in GitHub Desktop.
Printing Bytes Passing through a Pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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