-2

I've got a slice of pointers to structs (say []*A), these structs also internally have values and pointers say:

type A struct {
    a int
    b *B
}

type B struct {
    b int
}

Using copy on a []*A seems to do a deep copy.

sliceCopy := make([]*A, len(originalSlice))
copy(sliceCopy, originalSlice)

Is this correct? Can I assume it will always do a deep copy so I can use it in prod. Couldn't find an explanation online...

Edit: how would I do a deep copy of a slice, can I use reflection?

Martin
  • 748
  • 7
  • 27
  • 3
    There is no deep copy involved. `copy()` copies the elements. If elements are of a pointer type, it'll copy pointer values. – icza May 30 '22 at 14:05
  • I just checked and you're correct. Updated the question: can I use reflection to do a deep copy? or what is the easiest way? @icza – Martin May 30 '22 at 14:07

0 Answers0