io.PipeWriter.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 PipeWriter.Close() function in Go language is used to close the writer. However, successive reads from the PipeReader i.e, read half of the pipe will not return any bytes and an EOF error is returned. 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 (w *PipeWriter) Close() error

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

Return value: It returns the content written before Close method. And successive reads from the PipeReader i.e read half of the pipe will not return any bytes after calling Close() method and an EOF error will be returned.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.PipeWriter.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 Close method in go function after
    // two Write operations
    go func() {
        pipeWriter.Write([]byte("GfG"))
        pipeWriter.Write([]byte("GeeksforGeeks"))
        pipeWriter.Close()
  
        // Again calling Write method
        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 < 4; 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:

GfG
GeeksforGeeks
panic: EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox342418232/prog.go:42 +0x2d1

Here, the content of the two Write operations are returned as they are written before the Close() method but the content written after the Close method is not returned and an EOF error is thrown instead.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.PipeWriter.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 Close method in go function
    go func() {
        pipeWriter.Close()
  
        // Calling Write method
        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 < 4; 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: EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox173042396/prog.go:40 +0x2d1

Here, no Write operation is performed before Close method so no content is returned and an EOF error is thrown.




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.