Golang bytes.Buffer.Read() function example

package bytes

Read reads the total length of the input bytes from the buffer or until the buffer is drained. The return value n (2nd parameter) is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless the input length is zero); otherwise it is nil.

Golang bytes.Buffer.Read() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 buff := bytes.NewBufferString("abcdef")

 var threebytes [3]byte

 n, err := buff.Read(threebytes[0:3])
 fmt.Printf("%v  %s\n", err, string(threebytes[:n]))


 n, err = buff.Read(threebytes[:])
 fmt.Printf("%v  %s\n", err, string(threebytes[:n]))

 n, err = buff.Read(threebytes[:])
 fmt.Printf("%v  %s\n", err, string(threebytes[:n]))

 }

Output :

abc

def

EOF

Reference :

http://golang.org/pkg/bytes/#Buffer.Read

Advertisement