Golang bytes.Buffer.WriteString() function example

package bytes

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

Golang bytes.Buffer.WriteString() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

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

 n, err := buff.WriteString("Hello World! = 你好世界!")

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

Output :

Hello World! = 你好世界! 28 <nil>

Reference :

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

Advertisement