Standard library / bytes

bytes.Buffer.Read()

Reads up to len(p) bytes, consuming them; EOF is returned on an empty buffer for nonempty p. Expected stdout: '2 <nil> go\n'.

Formal Syntax & Structure

go syntaxgo
func (b *Buffer) Read(p []byte) (n int, err error)

Parameters & Attributes

NameTypeDescription
p[]byteWritable destination slice; its length limits bytes read.

Return Value

(n int, err error)

Reads up to len(p) bytes, consuming them; EOF is returned on an empty buffer for nonempty p.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt")
func main() {  b := bytes.NewBufferString("go")  p := make([]byte, 4)  n, e := b.Read(p)  fmt.Println(n, e, string(p[:n]))}
Go Reference