Golang bytes.Buffer.UnreadByte() function example

package bytes

UnreadByte unreads the last byte returned by the most recent read operation. If write has happened since the last read, UnreadByte returns an error.

Golang bytes.Buffer.UnreadByte() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

 buff := bytes.NewBuffer([]byte("abcdefg"))

 fmt.Println(string(buff.Next(3))) // read next 3 bytes


 fmt.Println(buff.String()) // starts from d

 buff.UnreadByte() // reset read position back to c (1 byte)
 fmt.Println(buff.String())
 }

Output :

abc

defg

cdefg

Reference :

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

Advertisement