Standard library / io

io.LimitedReader

Tracks underlying R and remaining N; stops at N bytes, including EOF when N reaches zero. Expected stdout: 'a 0\n'.

Formal Syntax & Structure

go syntaxgo
type LimitedReader struct {  R Reader  N int64}

Working Example

Example Snippetgo
package main
import (  "fmt"  "io"  "strings")
func main() {  r := &io.LimitedReader{R: strings.NewReader("abc"), N: 1}  b, _ := io.ReadAll(r)  fmt.Println(string(b), r.N)}
Go Reference