Standard library / io

io.ReadAtLeast()

Fills at least min bytes, possibly more up to buffer length. Short input after some bytes produces ErrUnexpectedEOF; min greater than buffer length produces ErrShortBuffer. Expected stdout: '3 abc true\n'.

Formal Syntax & Structure

go syntaxgo
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)

Parameters & Attributes

NameTypeDescription
rReaderSource reader.
buf[]byteCaller-supplied byte buffer.
minintMinimum required bytes.

Return Value

(n int, err error)

Fills at least min bytes, possibly more up to buffer length.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io"  "strings")
func main() {  b := make([]byte, 4)  n, e := io.ReadAtLeast(strings.NewReader("abc"), b, 4)  fmt.Println(n, string(b[:n]), e == io.ErrUnexpectedEOF)}
Go Reference