io.PipeReader.Read() 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 PipeReader.Read() function in Go language is used to implement the standard interface of the Read. It reads information from the pipe and blocks it until a writer appears or the write end of the pipe is closed. 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 (r *PipeReader) Read(data []byte) (n int, err error)

Here, “r” is a pointer to the PipeReader. Where, PipeReader is the read half of the pipe and “data” is a byte slice of specified length and the written data is read into it.

Return value: It returns the number of bytes read and an error if any. However, if the write end of the pipe is closed with an error then that error is returned as err else the err returned is an EOF error.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.PipeReader.Read() function
  
// Including main package
package main
  
// Importing fmt and io
import (
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Writing data to the pipe
    go func() {
        pipeWriter.Write([]byte("GfG"))
        pipeWriter.Write([]byte("GeeksforGeeks"))
        pipeWriter.Write([]byte("GfG is a CS-Portal."))
  
        // Closing write half of the pipe
        pipeWriter.Close()
  
        // Again calling Write method
        pipeWriter.Write([]byte("Author!"))
    }()
  
    // Defining data parameter of Read method
    data := make([]byte, 20)
  
    // Using for loop
    for i := 0; i < 3; i++ {
  
        // Calling pipeReader.Read() method
        n, err := pipeReader.Read(data)
  
        // If error is not nil panic
        if err != nil {
            panic(err)
        }
  
        // Prints the content read in buffer
        fmt.Printf("%s\n", data[:n])
  
        // Prints number of bytes read
        fmt.Printf("%v\n", n)
    }
}

chevron_right


Output:

GfG
3
GeeksforGeeks
13
GfG is a CS-Portal.
19

Here, no error is returned as the write end of the pipe is not closed till the “for” loop runs.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.PipeReader.Read() function
  
// Including main package
package main
  
// Importing fmt and io
import (
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Writing data to the pipe
    go func() {
        pipeWriter.Write([]byte("GfG"))
        pipeWriter.Write([]byte("GeeksforGeeks"))
        pipeWriter.Write([]byte("GfG is a CS-Portal."))
  
        // Closing write half of the pipe
        pipeWriter.Close()
  
        // Again calling Write method
        pipeWriter.Write([]byte("Author!"))
    }()
  
    // Defining data parameter of Read method
    data := make([]byte, 20)
  
    // Using for loop
    for i := 0; i < 4; i++ {
  
        // Calling pipeReader.Read() method
        n, err := pipeReader.Read(data)
  
        // If error is not nil panic
        if err != nil {
            panic(err)
        }
  
        // Prints the content read in buffer
        fmt.Printf("%s\n", data[:n])
  
        // Prints number of  bytes read
        fmt.Printf("%v\n", n)
    }
}

chevron_right


Output:

GfG
3
GeeksforGeeks
13
GfG is a CS-Portal.
19
panic: EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox634835876/prog.go:43 +0x364

Here, an EOF error is returned as the write end of the pipe is closed after the third iteration of the for loop.




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.