Open In App

io.PipeWriter.Write() Function in Golang with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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.Write() function in Go language is used to implement the standard interface of the Write. It writes information to the pipe and blocks it until one reader or more than one reader has taken up all the information or the read 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 (w *PipeWriter) Write(data []byte) (n int, err error)

Here, “w” is a pointer to the PipeWriter. Where PipeWriter is the write half of the pipe and “data” is a byte slice where the data is written.

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

Example 1:




// Golang program to illustrate the usage of
// io.pipeWriter.Write() function
  
// Including main package
package main
  
// Importing fmt and io
import (
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Defining data parameter of Read method
    data := make([]byte, 20)
  
    // Reading data into the buffer stated
    go func() {
        pipeReader.Read(data)
  
        // Closing read half of the pipe
        pipeReader.Close()
    }()
  
    // Using for loop
    for i := 0; i < 1; i++ {
  
        // Calling pipeWriter.Write() method
        n, err := pipeWriter.Write([]byte("GfG!"))
  
        // If error is not nil panic
        if err != nil {
            panic(err)
        }
  
        // Prints the content written
        fmt.Printf("%v\n", string(data))
  
        // Prints the number of bytes
        fmt.Printf("%v\n", n)
    }
}


Output:

GfG!
4

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

Example 2:




// Golang program to illustrate the usage of
// io.pipeWriter.Write() function
  
// Including main package
package main
  
// Importing fmt and io
import (
    "fmt"
    "io"
)
  
// Calling main
func main() {
  
    // Calling Pipe method
    pipeReader, pipeWriter := io.Pipe()
  
    // Defining data parameter of Read method
    data := make([]byte, 20)
  
    // Reading data into the buffer stated
    go func() {
        pipeReader.Read(data)
  
        // Closing read half of the pipe
        pipeReader.Close()
    }()
  
    // Using for loop
    for i := 0; i < 2; i++ {
  
        // Calling pipeWriter.Write() method
        n, err := pipeWriter.Write([]byte("GfG!"))
  
        // If error is not nil panic
        if err != nil {
            panic(err)
        }
  
        // Prints the content written
        fmt.Printf("%v\n", string(data))
  
        // Prints the number of bytes
        fmt.Printf("%v\n", n)
    }
}


Output:

GfG!
4
panic: io: read/write on closed pipe

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

Here, an ErrClosedPipe error is returned as the read end of the pipe is closed after the first iteration of the for loop.



Last Updated : 10 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads