Skip to content

Instantly share code, notes, and snippets.

@kerkerj
Created January 15, 2021 07:11
Show Gist options
  • Save kerkerj/6c4c6693a2066ed45d23fa8282ae52b6 to your computer and use it in GitHub Desktop.
Save kerkerj/6c4c6693a2066ed45d23fa8282ae52b6 to your computer and use it in GitHub Desktop.
Capture fmt.Println
func CaptureStdout(f func()) string {
r, w, _ := os.Pipe()
stdout := os.Stdout
os.Stdout = w
defer func() {
os.Stdout = stdout
}()
f()
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment