Golang bytes.Buffer.ReadFrom() function example

package bytes

ReadFrom reads data from input buffer until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with ErrTooLarge.

Golang bytes.Buffer.ReadFrom() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

 bufferA := bytes.NewBuffer([]byte("abcd"))

 bufferB := bytes.NewBuffer(nil)

 n, err := bufferB.ReadFrom(bufferA)

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

 }

Output :

<nil> abcd 4

Reference :

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

Advertisement