Golang bytes.Buffer.UnreadRune() function example

package bytes

UnreadRune unreads the last rune returned by ReadRune. If the most recent read or write operation on the buffer was not a ReadRune, UnreadRune returns an error. (In this regard it is stricter than UnreadByte, which will unread the last byte from any read operation.)

Golang bytes.Buffer.UnreadRune() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

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

 rune, _, _ := buff.ReadRune()

 fmt.Println(string(rune))

 // remainder runes

 fmt.Println(string(buff.Bytes()))

 buff.UnreadRune()

 // back to beginning
 fmt.Println(string(buff.Bytes()))

 }

Output :

a

bcdefg

abcdefg

Reference :

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

Advertisement