io.PipeReader.Close() 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.Close() function in Go language is used to close the reader. However, successive writes to the PipeWriter i.e, write half of the pipe will return the ErrClosedPipe error. 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) Close() error

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

Return value: It returns the content written before calling Close method. And successive writes to the PipeWriter i.e, write half of the pipe will return the ErrClosedPipe error.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.pipeReader.Close() function
  
// Including main package
package main
  
// Importing fmt and io
import (
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Calling Write method in go function
    go func() {
        pipeWriter.Write([]byte("GfG"))
        pipeWriter.Write([]byte("GeeksforGeeks"))
        pipeWriter.Write([]byte("GfG is a CS-Portal."))
    }()
  
    // Creating buffer using make keyword
    // of specified length
    buffer := make([]byte, 50)
  
    // Using for loop
    for i := 0; i < 2; i++ {
  
        // Reading the contents in buffer
        n, err := pipeReader.Read(buffer)
  
        // If error is not nil panic
        if err != nil {
            panic(err)
        }
  
        // Calling Close method
        pipeReader.Close()
  
        // Prints the content read in buffer
        fmt.Printf("%s\n", buffer[:n])
    }
}

chevron_right


Output:

GfG
panic: io: read/write on closed pipe

goroutine 1 [running]:
main.main()
    /tmp/sandbox180013044/prog.go:38 +0x302

Here, the content of only one Write operations is returned as its written before Close() method but the content written after the Close method is not returned and an ErrClosedPipe error is thrown instead.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.pipeReader.Close() function
  
// Including main package
package main
  
// Importing fmt and io
import (
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Calling Write method in go function
    go func() {
        pipeWriter.Write([]byte("GfG"))
        pipeWriter.Write([]byte("GeeksforGeeks"))
        pipeWriter.Write([]byte("GfG is a CS-Portal."))
    }()
  
    // Creating buffer using make keyword
    // of specified length
    buffer := make([]byte, 50)
  
    // Calling Close method
    pipeReader.Close()
  
    // Using for loop
    for i := 0; i < 2; i++ {
  
        // Reading the contents in buffer
        n, err := pipeReader.Read(buffer)
  
        // If error is not nil panic
        if err != nil {
            panic(err)
        }
  
        // Prints the content read in buffer
        fmt.Printf("%s\n", buffer[:n])
    }
}

chevron_right


Output:

panic: io: read/write on closed pipe

goroutine 1 [running]:
main.main()
    /tmp/sandbox881512815/prog.go:41 +0x2ea

Here, nothing is read into the buffer as Close() method is called before any read operation so no content is read and only error is thrown here.




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.