Built-in function

Go copy()

Copies up to the smaller length of destination and source slices, even when they overlap. A byte slice can also copy from a string. Capacity beyond destination length is not copied into. Example prints 2 [8 9].

Formal Syntax & Structure

go syntaxgo
copy(dst, src)

Parameters & Attributes

NameTypeDescription
dstsliceDestination.
srccompatible slice or string for []byteSource.

Return Value

int

Number of copied elements or bytes.

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  dst := make([]int, 2)  n := copy(dst, []int{8, 9, 10})  fmt.Println(n, dst)}
Go Reference