Golang reflect.Copy() function example

package reflect

Golang reflect.Copy() function usage example

 package main

 import (
  "fmt"
  "reflect"
 )

 func main() {
  oldSlice := []string{"abc", "def"}
  newSlice := []string{"efg", "hij", "klm"}

  // will overwrite "efg" and "hij"

  var n = reflect.Copy(reflect.ValueOf(newSlice), reflect.ValueOf(oldSlice))

  fmt.Printf("Copied %d elements.\n", n)
  fmt.Printf("newSlice elements are now : %v \n", newSlice)

 }

Reference :

http://golang.org/pkg/reflect/#Copy

Advertisement