io.ReadAtLeast() 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
ReadAtLeast() function in Go language is used to read from the stated reader "r" into the stated buffer "buf" until it has read for at least the minimum number of stated bytes. 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 ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
Here, "r" is the reader stated, "buf" is the buffer stated, and "min" is the minimum number of bytes until which the reader reads into the given buffer.
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 minimum number of bytes. Here, "n" returned will be greater than the "min" bytes 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 than the stated "min" bytes then this method returns
ErrUnexpectedEOF error. But, if the stated minimum number of bytes is more than the length of the stated buffer then this method returns
ErrShortBuffer error. However, if the stated reader returns an error after reading at least the stated minimum bytes then the error is declined.
Example 1:
C
// Golang program to illustrate the usage of
// io.ReadAtLeast() 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 ReadAtLeast method with its parameters
n, err := io.ReadAtLeast(reader, buffer, 3)
// 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: 5
Content in buffer: Geeks
Here, the 'n' returned i.e, 5 is greater than 'min' i.e, 3 as an error is nil.
Example 2:
C
// Golang program to illustrate the usage of
// io.ReadAtLeast() 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("GeeksforGeeks")
// Defining buffer of specified length
// using make keyword
buffer := make([]byte, 4)
// Calling ReadAtLeast method with its parameters
n, err := io.ReadAtLeast(reader, buffer, 5)
// 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: short buffer
goroutine 1 [running]:
main.main()
/tmp/sandbox041442440/prog.go:29 +0x20f
Here, the buffer stated in the above code has length lesser than the "min" bytes stated so an error is thrown.
Similar Reads
io.ReadFull() 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 ReadFull() function in Go language is used to read from the stated reader "r" into the stated buffer "buf" and the bytes co
3 min read
io.TeeReader() 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 TeeReader() function in Go language is used to return a "Reader" that reads from the stated "r" and then writes it to the s
3 min read
reflect.Int() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Int() Function in Golang is used to get the v's underlying value, as an int64. To access this function, one needs
1 min read
io.LimitReader() 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 LimitReader() function in Go language is used to return a "Reader" that reads from the stated "r" but it pauses if the EOF
2 min read
io.MultiReader() 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 MultiReader() function in Go language is used to return a "Reader" that is the logical concatenation of all the stated inpu
3 min read
reflect.IsZero() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.IsZero() Function in Golang is used to check whether v is the zero value for its type. To access this function, o
1 min read
reflect.Kind() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Kind() Function in Golang is used to find the name of kind. To access this function, one needs to imports the ref
2 min read
io.PipeReader.Read() 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.Read() function in Go language is used to implement the standard interface of the Read. It reads information fro
3 min read
reflect.Len() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Len() Function in Golang is used to get the v's length. To access this function, one needs to imports the reflect
1 min read
reflect.Index() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Index() Function in Golang is used to get the v's i'th element. To access this function, one needs to imports the
2 min read