reflect.Copy() Function in Golang with Examples Last Updated : 28 Apr, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Copy() Function in Golang is used to copies the contents of the source into destination until either destination has been filled or source has been exhausted. To access this function, one needs to imports the reflect package in the program. Syntax: func Copy(dst, src Value) int Parameters: This function takes two parameters of Slice or Array type. And dst and src must have the same element type. Return Value: This function returns the number of elements copied. Below examples illustrate the use of above method in Golang: Example 1: C // Golang program to illustrate // reflect.Copy() Function package main import ( "fmt" "reflect" ) // Main function func main() { // Source src := reflect.ValueOf([]int{10, 20, 32}) /* make sure the dest space is larger than src */ // destination dest := reflect.ValueOf([]int{1, 2, 3}) // To copy Copy() function is used // and it returns the number of // elements copied cnt := reflect.Copy(dest, src) data := dest.Interface().([]int) data[0] = 100 // printing the values fmt.Println("Number of element Copied :", cnt) fmt.Println("Source :", src) fmt.Println("destination :", dest) } Output: Number of element Copied : 3 Source : [10 20 32] destination : [100 20 32] Example 2: C // Golang program to illustrate // reflect.Copy() Function package main import ( "fmt" "reflect" ) // Struct with two int value type temp struct { A0 []int A1 []int } // Main function func main() { var val temp // Source val.A0 = append(val.A0, []int{1, 2, 3, 4, 5, 6, 7, 8, 9}...) // destination val.A1 = append(val.A1, 9, 8, 7, 6) // To copy Copy() function is used // and it returns the number of // elements copied var n = reflect.Copy(reflect.ValueOf(val.A0), reflect.ValueOf(val.A1)) // printing the values fmt.Println("Number of element Copied :", n) fmt.Println("{Source, destination} :", val) } Output: Number of element Copied : 4 {Source, destination} : {[9 8 7 6 5 6 7 8 9] [9 8 7 6]} Comment More infoAdvertise with us Next Article reflect.Copy() Function in Golang with Examples S SHUBHAMSINGH10 Follow Improve Article Tags : Go Language Golang-reflect Similar Reads reflect.Cap() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Cap() Function in Golang is used to get the v's capacity. To access this function, one needs to imports the refle 1 min read reflect.Close() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Close() Function in Golang is used to close the channel v. To access this function, one needs to imports the refl 2 min read reflect.Complex() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Complex() Function in Golang is used to get the v's underlying value. To access this function, one needs to impor 1 min read reflect.Call() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Call() Function in Golang is used to calls the function v with the input arguments in. To access this function, o 2 min read reflect.ChanOf() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.ChanOf() Function in Golang is used to get the channel type with the given direction and element type, i.e., t re 1 min read Like