Skip to content

Instantly share code, notes, and snippets.

@duglin
Last active August 29, 2015 14:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
package main
import (
"bytes"
"fmt"
"io"
"os"
)
func main() {
stdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() { os.Stdout = stdout }()
done := make(chan bool)
var testOutput bytes.Buffer
go func() {
io.Copy(&testOutput, r)
done <- true
}()
fmt.Printf("Hello world")
os.Stdout.Close()
<-done
os.Stdout = stdout
fmt.Printf("RESULT: %s\n", testOutput.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment