io.Pipe() Function in Golang with Examples

In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The Pipe() function in Go language is used to create a concurrent in-memory pipe and can be applied in order to link the code that expects an io.Reader with the code that expects an io.Writer. Here, the Reads and Writes on the pipe are paired one-to-one except when more than one “Reads” are required to take a single “Write”. This indicates that each Write to the PipeWriter stops until it has satiated one or more than one Reads from the PipeReader that completely takes the written data.

However, the data is traced right away from the Write to the related Read(s) and there is no buffering internally. Moreover, this function is defined under the io package. Here, you need to import the “io” package in order to use these functions.

Syntax:

func Pipe() (*PipeReader, *PipeWriter)

Here, “PipeReader” is a pointer to the PipeReader. Where PipeReader is the read half of a pipe and “PipeWriter” is a pointer to the PipeWriter. Where PipeWriter is the write half of a pipe.

Return value: It returns a pointer to the PipeReader and PipeWriter.



Note: It is secured to call Read and Write simultaneously or with a Close. However, parallel calls to Read, and parallel calls to Write are also secured. The separate calls will be closed sequentially.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.Pipe() function
  
// Including main package
package main
  
// Importing fmt, io, and bytes
import (
    "bytes"
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Using Fprint in go function to write
    // data to the file
    go func() {
        fmt.Fprint(pipeWriter, "Geeks\n")
  
        // Using Close method to close write
        pipeWriter.Close()
    }()
  
    // Creating a buffer
    buffer := new(bytes.Buffer)
  
    // Calling ReadFrom method and writing
    // data into buffer
    buffer.ReadFrom(pipeReader)
  
    // Prints the data in buffer
    fmt.Print(buffer.String())
}

chevron_right


Output:

Geeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.Pipe() function
  
// Including main package
package main
  
// Importing fmt, io, and bytes
import (
    "bytes"
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Using Fprint in go function to write
    // data to the file
    go func() {
        fmt.Fprint(pipeWriter, "GeeksforGeeks\nis\na\nCS-Portal.\n")
  
        // Using Close method to close write
        pipeWriter.Close()
    }()
  
    // Creating a buffer
    buffer := new(bytes.Buffer)
  
    // Calling ReadFrom method and writing
    // data into buffer
    buffer.ReadFrom(pipeReader)
  
    // Prints the data in buffer
    fmt.Print(buffer.String())
}

chevron_right


Output:

GeeksforGeeks
is
a
CS-Portal.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.