Standard library / os

os.Pipe()

Creates connected read and write files; close both ends, and close the writer to signal EOF to readers. Example uses a disposable temporary workspace. Expected stdout: 'abc\n'.

Formal Syntax & Structure

go syntaxgo
func Pipe() (r *File, w *File, err error)

Return Value

(r *File, w *File, err error)

Creates connected read and write files; close both ends, and close the writer to signal EOF to readers.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io"  "os")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  r, w, e := os.Pipe()  check(e)  defer r.Close()  _, e = w.WriteString("abc")  check(e)  check(w.Close())  b, e := io.ReadAll(r)  check(e)  fmt.Println(string(b))}
Go Reference