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
| Name | Type | Description |
|---|---|---|
| dst | Writer | Destination writer. |
| src | Reader | Source reader. |
| n | int64 | Requested 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)}