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:
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:
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.