Skip to content

Instantly share code, notes, and snippets.

@DavadDi
Created June 16, 2017 03:52
Show Gist options
  • Save DavadDi/10dbe6887c6061c7af8423bb393273f4 to your computer and use it in GitHub Desktop.
Save DavadDi/10dbe6887c6061c7af8423bb393273f4 to your computer and use it in GitHub Desktop.
import (
"bytes"
"io"
"os"
)
// not thread safe
func captureStdout(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = old
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