Golang bytes.Buffer.Truncate() function example

package bytes

Truncate discards all but the first n (1st parameter) unread bytes from the buffer. It panics if n is negative or greater than the length of the buffer.

 Golang bytes.Buffer.Truncate() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

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

 buff.Truncate(2) // keep first 2 bytes and discard the rest

 fmt.Println(buff.String())

 }

Output :

ab

Reference :

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

Advertisement