Golang builtin.copy() function example

package builtin

The copy built-in function copies elements from a source slice into a destination slice.

Golang builtin.copy() function usage example

 package main

 import "fmt"

 func main() {
 source := []string{"Hi","Hello","Hey"}

 fmt.Println("Source : ", source[:])

 destination := make([]string, 5)

 fmt.Println("Destination before copy :", destination[:])

 num := copy(destination, source[:])

 fmt.Println("Destination AFTER copy :", destination[:])

 fmt.Println("Element(s) copied : ", num)
 }

Output :

Source : [Hi Hello Hey]

Destination before copy : [ ]

Destination AFTER copy : [Hi Hello Hey ]

Element(s) copied : 3

Reference :

http://golang.org/pkg/builtin/#copy

Advertisement