-
-
Save nakayama900/94ecad0410e65f63eae2116f14652be9 to your computer and use it in GitHub Desktop.
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 mock4std | |
import ( | |
"io" | |
"os" | |
"sync" | |
) | |
type RW struct { | |
R io.Reader | |
W io.WriteCloser | |
} | |
type pipePairs = []*RW | |
func genPipePair() pipePairs { | |
r1, w2 := io.Pipe() | |
r2, w1 := io.Pipe() | |
return pipePairs{ | |
&RW{r1, w1}, | |
&RW{r2, w2}, | |
} | |
} | |
func StdConnect( | |
solver func(io.Reader, io.Writer) error, | |
server func(io.Reader, io.Writer) error, | |
) { | |
var wg *sync.WaitGroup | |
var pipes = genPipePair() | |
wg.Add(2) | |
go loggedIoWorker(solver, wg, pipes[0]) | |
go loggedIoWorker(server, wg, pipes[1]) | |
wg.Wait() | |
} | |
func loggedIoWorker( | |
f func(io.Reader, io.Writer) error, | |
wg *sync.WaitGroup, | |
RW *RW, | |
) { | |
defer wg.Done() | |
defer RW.W.Close() | |
var tee = io.TeeReader(RW.R, os.Stdout) | |
f(tee, RW.W) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment