Golang bytes.Buffer.Write() function example

package bytes

Write appends the contents of the given input to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.

Golang bytes.Buffer.Write() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 buff := bytes.NewBuffer(nil) // create empty buffer

 n, err := buff.Write([]byte("abc"))


 fmt.Printf("%s %d %v \n", string(buff.Bytes()), n, err)
 }

Output :

abc 3 <nil>

Reference :

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

Advertisement