Golang bufio.ReadBytes() function example

package bufio

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 delimiter. For simple uses, a Scanner may be more convenient.

Golang bufio.ReadBytes() function usage example

 readbuffer := bytes.NewBuffer([]byte("abcd#fg"))
 reader := bufio.NewReader(readbuffer)
 buffer, err := reader.ReadBytes('#') // # is our delimiter
 fmt.Printf("%s \n", string(buffer))

Output :

abcd

Advertisement