Last active
June 5, 2022 12:11
-
-
Save narenaryan/661e3196d50a35d9ec44428608a40da9 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
| 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