Standard library / io

io.Pipe()

Creates a synchronous in-memory pipe with no internal buffer; coordinate the reader and writer, and close both ends. Expected stdout: 'ok <nil>\n'.

Formal Syntax & Structure

go syntaxgo
func Pipe() (*PipeReader, *PipeWriter)

Return Value

(*PipeReader, *PipeWriter)

Creates a synchronous in-memory pipe with no internal buffer; coordinate the reader and writer, and close both ends.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io")
func main() {  r, w := io.Pipe()  defer r.Close()  go func() { defer w.Close(); w.Write([]byte("ok")) }()  b, e := io.ReadAll(r)  fmt.Println(string(b), e)}
Go Reference