Skip to content
Related Articles

Related Articles

Improve Article

io.ReadFull() Function in Golang with Examples

  • Last Updated : 05 May, 2020

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 ReadFull() function in Go language is used to read from the stated reader “r” into the stated buffer “buf” and the bytes copied is exactly equal to the length of the buffer specified. 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 ReadFull(r Reader, buf []byte) (n int, err error)

Here, “r” is the reader stated, “buf” is the buffer stated of the specified length.

Return value: It returns the number of bytes that the stated buffer copies and also returns an error if the number of bytes reads are less than the length of the buffer specified. Here, “n” returned will be equal to the length of the buffer specified if and only if the error is nil. However, the error returned is “EOF” if and only if no bytes are read.

Note: If an EOF takes place after reading fewer bytes but not all the bytes then this method returns an ErrUnexpectedEOF error. However, if the stated reader returns an error after reading at least length of the buffer then the error is declined.



Example 1:




// Golang program to illustrate the usage of
// io.ReadFull() function
  
// Including main package
package main
  
// Importing fmt, io, and strings
import (
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("Geeks")
  
    // Defining buffer of specified length
    // using make keyword
    buffer := make([]byte, 4)
  
    // Calling ReadFull method with its parameters
    n, err := io.ReadFull(reader, buffer)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("Number of bytes in the buffer: %d\n", n)
    fmt.Printf("Content in buffer: %s\n", buffer)
}

Output:

Number of bytes in the buffer: 4
Content in buffer: Geek

Here, the ‘n’ returned i.e, 4 is equal to the length of ‘buf’ as the error is nil.

Example 2:




// Golang program to illustrate the usage of
// io.ReadFull() function
  
// Including main package
package main
  
// Importing fmt, io, and strings
import (
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("Geeks")
  
    // Defining buffer of specified length
    // using make keyword
    buffer := make([]byte, 6)
  
    // Calling ReadFull method with its parameters
    n, err := io.ReadFull(reader, buffer)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("Number of bytes in the buffer: %d\n", n)
    fmt.Printf("Content in buffer: %s\n", buffer)
}

Output:

panic: unexpected EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox503804944/prog.go:29 +0x210

Here, the buffer stated in above code has length greater than the bytes read from the reader so an EOF error is thrown.




My Personal Notes arrow_drop_up
Recommended Articles
Page :