Golang bytes.Buffer.WriteTo() function example

package bytes

WriteTo writes data to the given io.Writer interface (1st parameter) until the buffer is drained or an error occurs. The return value n is the number of bytes written; it always fits into an int, but it is int64 to match the io.WriterTo interface. Any error encountered during the write is also returned.

Golang bytes.Buffer.WriteTo() function usage example

 package main

 import (
  "bytes"
  "fmt"
 )

 func main() {
 sourcebuffer := bytes.NewBuffer([]byte("abc"))
 destbuffer := bytes.NewBuffer(nil)

 n, err := sourcebuffer.WriteTo(destbuffer)

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

Output :

abc 3 <nil>

Reference :

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

Advertisement