Created
January 15, 2021 07:11
-
-
Save kerkerj/6c4c6693a2066ed45d23fa8282ae52b6 to your computer and use it in GitHub Desktop.
Capture fmt.Println
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
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