Standard library / io

io.CopyN()

Copies exactly n bytes or returns an error. Check both the count and error when the source ends early. Expected stdout: '2 ab <nil>\n'.

Formal Syntax & Structure

go syntaxgo
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)

Parameters & Attributes

NameTypeDescription
dstWriterDestination writer.
srcReaderSource reader.
nint64Requested byte count or bound.

Return Value

(written int64, err error)

Copies exactly n bytes or returns an error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "io"  "strings")
func main() {  var b bytes.Buffer  n, e := io.CopyN(&b, strings.NewReader("abc"), 2)  fmt.Println(n, b.String(), e)}
Go Reference