Golang bufio.WriteString() function example

package bufio

WriteString writes a string. It returns the number of bytes written and it also returns an error if something goes wrong.

Golang bufio.WriteString() function usage example

 package main

 import (
 "bufio"
 "bytes"
 "fmt"
 )

 func main() {
 writebuffer := bytes.NewBuffer(nil)
 writer := bufio.NewWriter(writebuffer)

 num_bytes, _ := writer.WriteString("what is your 妈妈's maiden name?") // ignore the error, but you can put in error handler if you want

 writer.Flush()
 fmt.Println(string(writebuffer.Bytes()))
 fmt.Printf("string has %d bytes\n",  num_bytes)

 }

Output :

what is your 妈妈's maiden name?

string has 34 bytes

Reference :

http://golang.org/pkg/bufio/#Writer.WriteString

Advertisement