Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Created June 5, 2022 11:51
Show Gist options
  • Save narenaryan/9fac30449a3721989ffd64e8a3148e2f to your computer and use it in GitHub Desktop.
Save narenaryan/9fac30449a3721989ffd64e8a3148e2f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
)
func main() {
pr, pw := io.Pipe()
// Writing data to writer should be in a go-routine
// because pipe is synchronous.
go func() {
defer pw.Close() // Important! To notify writing is done
fmt.Fprintln(pw, "Hello Medium")
}()
// Code is blocked until someone writes to writer and closes it
b, err := io.ReadAll(pr)
if err != nil {
panic(err)
}
// Optional: verify data
fmt.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment