Golang bytes.Buffer.Next() function example

package bytes

Next returns a slice containing the next n (1st parameter) bytes from the buffer, advancing the buffer as if the bytes had been returned by Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The slice is only valid until the next call to a read or write method.

Golang bytes.Buffer.Next() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

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

 var threebytes [3]byte

 buff.Read(threebytes[0:3])

 fmt.Printf("%s\n", string(threebytes[:]))

 // read the next 5 bytes

 fmt.Printf("%s\n", string(buff.Next(5)))


 }

Output :

abc

defgh

Reference :

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

Advertisement