Golang bytes.Buffer.NewBufferString() function example

package bytes

NewBufferString creates and initializes a new Buffer using the input string as its initial contents. It is intended to prepare a buffer to read an existing string.


In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

Golang bytes.NewBufferString() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 newbuf := bytes.NewBufferString("abcdefg") // just a string will do

 var buff [7]byte

 newbuf.Read(buff[:])

 fmt.Println(string(buff[:]))
 }

Output :

abcdefg

Reference :

http://golang.org/pkg/bytes/#NewBufferString

Advertisement