Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Last active June 5, 2022 12:11
Show Gist options
  • Select an option

  • Save narenaryan/661e3196d50a35d9ec44428608a40da9 to your computer and use it in GitHub Desktop.

Select an option

Save narenaryan/661e3196d50a35d9ec44428608a40da9 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io"
"os"
)
// Your function
func foo(w *io.PipeWriter) {
defer w.Close()
// Write a message to pipe writer
fmt.Fprintln(w, "Hello Medium")
}
func main() {
// Create a pipe
pr, pw := io.Pipe()
// Pass writer to function
go foo(pw)
// Variable to get standard output of function
var b bytes.Buffer
// Create a multi writer that is a combination of
// os.Stdout and our variable byte buffer
mw := io.MultiWriter(os.Stdout, &b)
// Copies reader content to standard output
_, err := io.Copy(mw, pr)
if err != nil {
panic(err)
}
// Optional: verify data
fmt.Println(b.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment