While learning nuances of array data structure in Golang I came across an interesting confusion. I have learnt from blog.golang.org that -
when you assign or pass around an array value you will make a copy of its contents.
To check this myself I wrote the following piece of code:
package main
import "fmt"
func main() {
x := []int{2, 4, 5}
y := x
y[0] = -10
// expecting 2 here but getting -10, since y := x is supposed to be a content copy
fmt.Println(x[0])
// not same
println("&x: ", &x)
println("&y: ", &y)
// same
println("&x[0]: ", &x[0])
println("&y[0]: ", &y[0])
}
Doing exactly same thing is java I got same internal object address for x and y as expected.
Go Code: https://play.golang.org/p/04l0l84eT4J
Java code: https://pastebin.ubuntu.com/p/S3fHMTj5NC/