Created
June 16, 2017 03:52
-
-
Save DavadDi/10dbe6887c6061c7af8423bb393273f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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