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
| Name | Type | Description |
|---|---|---|
| dst | Writer | Destination writer. |
| src | Reader | Source reader. |
| buf | []byte | Caller-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)}