Looking for go Answers? Try Ask4KnowledgeBase
Looking for go Keywords? Try Ask4Keywords

Go Copying contents from one slice to another slice


Example

If you wish to copy the contents of a slice into an initially empty slice, following steps can be taken to accomplish it-

  1. Create the source slice:
var sourceSlice []interface{} = []interface{}{"Hello",5.10,"World",true}
  1. Create the destination slice, with:
  • Length = Length of sourceSlice
var destinationSlice []interface{} = make([]interface{},len(sourceSlice))
  1. Now that the destination slice's underlying array is big enough to accomodate all the elements of the source slice, we can proceed to copy the elements using the builtin copy:
copy(destinationSlice,sourceSlice)