r, w := io.Pipe() go func() { w.Write([]byte("hello\n")) w.Close() }() buf := make([]byte, 100) n, err := r.Read(buf) if err != nil { log.Fatal(err) } fmt.Printf("%s", buf[:n]) // Output: hello
r, w := io.Pipe() go func() { w.Write([]byte("hello\n")) w.Close() }() n, err := w.WriteTo(os.Stdout) if err != nil { log.Fatal(err) }In this example, we again create a new pipe and launch a goroutine that writes the string "hello\n" to the pipe before closing it. Instead of reading from the pipe, we use the `WriteTo` method of the `PipeWriter` to write the contents of the pipe to `os.Stdout`. This method returns the number of bytes written, which we store in `n`. The package library for these functions is `io`.