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) ReaderParameters & Attributes
| Name | Type | Description |
|---|---|---|
| r | Reader | Source reader. |
| n | int64 | Requested 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)}