package main import ( "bytes" "fmt" ) func main() { buf := bytes.NewBufferString("hello world") b, err := buf.ReadBytes(' ') if err != nil { panic(err) } fmt.Println(string(b)) }
hello
package main import ( "bytes" "fmt" ) func main() { buf := bytes.NewBufferString("hello world") b, err := buf.ReadBytes('z') if err != nil { fmt.Println(err) } fmt.Println(string(b)) }
EOFIn the above examples, we are creating a new bytes.Buffer object using bytes.NewBufferString method. Then, we are using the ReadBytes method to read bytes from the buffer until we reach a specified delimiter (in the first example, space ' ' and in the second example, a non-existent character 'z'). The returned bytes are printed on the console using fmt.Println. Package library: bytes.