Standard library / io

io.LimitReader()

Stops delivering bytes after at most n bytes; does not report whether the underlying stream has extra data. Expected stdout: 'ab <nil>\n'.

Formal Syntax & Structure

go syntaxgo
func LimitReader(r Reader, n int64) Reader

Parameters & Attributes

NameTypeDescription
rReaderSource reader.
nint64Requested byte count or bound.

Return Value

Reader

Stops delivering bytes after at most n bytes; does not report whether the underlying stream has extra data.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io"  "strings")
func main() {  r := io.LimitReader(strings.NewReader("abcd"), 2)  b, e := io.ReadAll(r)  fmt.Println(string(b), e)}
Go Reference