Golang bytes.Buffer.ReadBytes() function example

package bytes

ReadBytes reads until the first occurrence of delimiter in the input, returning a slice containing the data up to and including the delimiter. If ReadBytes encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadBytes returns err != nil if and only if the returned data does not end in the given delimiter.

Golang bytes.Buffer.ReadBytes() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 buff := bytes.NewBufferString("abcd*ef")

 rb, err := buff.ReadBytes('*') // end with 's'.... plural!

 fmt.Printf("%v  %d  %s\n", err, rb, string(rb))


 }

Output :

[97 98 99 100 42] abcd*

Reference :

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

Advertisement