How to Use io.Pipe in Go

io.Pipe creates a connected reader and writer pair for streaming data between goroutines.

Use io.Pipe() to create a connected pair of io.Reader and io.Writer that share a buffer, allowing one goroutine to write data while another reads it simultaneously.

pr, pw := io.Pipe()

// Writer goroutine
func() {
    defer pw.Close()
    pw.Write([]byte("hello"))
}()

// Reader goroutine
func() {
    buf := make([]byte, 5)
    pr.Read(buf)
    fmt.Println(string(buf))
}()