Skip to content

Instantly share code, notes, and snippets.

@nakayama900
Last active March 30, 2023 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakayama900/94ecad0410e65f63eae2116f14652be9 to your computer and use it in GitHub Desktop.
Save nakayama900/94ecad0410e65f63eae2116f14652be9 to your computer and use it in GitHub Desktop.
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