title: Array Assignment in Go: Content copy or Content pointer copy
body: <p>While learning nuances of array data structure in Go I came across an interesting confusion. I have learnt from <a href="https://blog.golang.org/go-slices-usage-and-internals" rel="nofollow noreferrer" title="here">blog.golang.org</a> that -</p>
  <blockquote>
  <p>when you assign or pass around an array value you will
  make a copy of its contents.</p>
  </blockquote>
  <p>To check this myself I wrote the following piece of code:</p>
  <pre><code>package main
  
  import &quot;fmt&quot;
  
  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(&quot;&amp;x: &quot;, &amp;x)
      println(&quot;&amp;y: &quot;, &amp;y)
  
      // same
      println(&quot;&amp;x[0]: &quot;, &amp;x[0])
      println(&quot;&amp;y[0]: &quot;, &amp;y[0])
  }
  </code></pre>
  <p>Doing exactly the same thing in Java I got same internal object address for x and y as expected.</p>
  <p>Go Code: <a href="https://play.golang.org/p/04l0l84eT4J" rel="nofollow noreferrer">https://play.golang.org/p/04l0l84eT4J</a></p>
  <p>Java code: <a href="https://pastebin.ubuntu.com/p/S3fHMTj5NC/" rel="nofollow noreferrer">https://pastebin.ubuntu.com/p/S3fHMTj5NC/</a></p>