Standard library / io

io.CopyBuffer()

Like Copy with caller-supplied scratch space; nil allocates a buffer, non-nil empty panics. Fast paths can bypass it. Expected stdout: '3 abc <nil>\n'.

Formal Syntax & Structure

go syntaxgo
func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)

Parameters & Attributes

NameTypeDescription
dstWriterDestination writer.
srcReaderSource reader.
buf[]byteCaller-supplied byte buffer.

Return Value

(written int64, err error)

Like Copy with caller-supplied scratch space; nil allocates a buffer, non-nil empty panics.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "io"  "strings")
func main() {  var b bytes.Buffer  n, e := io.CopyBuffer(struct{ io.Writer }{&b}, struct{ io.Reader }{strings.NewReader("abc")}, make([]byte, 2))  fmt.Println(n, b.String(), e)}
Go Reference