Golang bufio.ReadString() function example

package bufio

ReadString reads until the first occurrence of delimiter in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString 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.ReadString() function usage example

 readbuffer := bytes.NewBuffer([]byte("abcde#fghijk"))

 reader := bufio.NewReader(readbuffer)

 str,_ := reader.ReadString('#') // # is the delimiter

 fmt.Println(string(str))

Output :

abcde#

Reference :

http://golang.org/pkg/bufio/#Reader.ReadLine

Advertisement