Skip to content

Instantly share code, notes, and snippets.

@duglin
Last active August 29, 2015 14:16
Show Gist options
  • Save duglin/139b2e6e02a866e339d5 to your computer and use it in GitHub Desktop.
Save duglin/139b2e6e02a866e339d5 to your computer and use it in GitHub Desktop.
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