-1

For the below array,

var a[2][3]int
a[0][0] = 55
a[0][1] = 56
a[0][2] = 57
a[1][0] = 65
a[1][1] = 66
a[1][2] = 67

on performing array copy,

a[0] = a[1]

Question:

Is the array(a[0]) copy a deep copy or shallow copy?

After copy, Does a[0] have separate values(3 int's) than a[1] values(3 int's)?

overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

3

It is a deep copy. An array in Go doesn't involve any pointers (unless it's an array of pointers, of course). Each variable of an array type has its own contiguous block of memory holding its values.

After your initialization code, a is a block of memory like this (just 6 ints in 6 consecutive memory words):

55 56 57 65 66 67

Then after the copy, it is like this:

65 66 67 65 66 67

There are two separate copies of the values.

(But slices are different. They do have pointers, and so they are normally copied shallowly.)

andybalholm
  • 15,395
  • 3
  • 37
  • 41
  • 1
    @overexchange: Start with the basic [documentation for Go](https://golang.org/ref/spec). Pointers are fundamental to the language, and you can't really use it without them. – JimB Jan 27 '17 at 13:55
  • Go has pointer types (as in `var p *int`), but in this case I'm talking about the implicit pointer that is part of a slice header. – andybalholm Jan 27 '17 at 14:06