-3

Why is matrix change as well? Go variables are passed or copied by value, not reference unless explicitly specified using pointers vs values. Is there something that I don't understand?


import "fmt"

func main() {
    matrix := [][]int{
        {100, 20, 30, 10, 11},
        {15, 100, 16, 4, 2},
        {3, 5, 100, 2, 4},
        {19, 6, 18, 100, 3},
        {16, 4, 7, 16, 100},
    }
    var matrix2 [][]int
    matrix2 = matrix
    matrix2[0][1] = 11111111
    fmt.Println(matrix)
    fmt.Println(matrix2)

    // Output :
    [[100 11111111 30 10 11] [15 100 16 4 2] [3 5 100 2 4] [19 6 18 100 3] [16 4 7 16 100]]
    [[100 11111111 30 10 11] [15 100 16 4 2] [3 5 100 2 4] [19 6 18 100 3] [16 4 7 16 100]]
}
Leo
  • 5
  • 2
  • 4
    Does this answer your question? [updating one position in a slice of slices results in multiple updates](https://stackoverflow.com/questions/69005086/updating-one-position-in-a-slice-of-slices-results-in-multiple-updates) –  Sep 05 '21 at 22:46
  • Not really. I want to understand why it is happening. – Leo Sep 05 '21 at 22:58
  • https://stackoverflow.com/questions/39993688/are-slices-passed-by-value/39993797#39993797 –  Sep 05 '21 at 23:05
  • to check for the underlying array of a slice https://stackoverflow.com/questions/36706843/how-to-get-the-underlying-array-of-a-slice-in-go/36707413#36707413 –  Sep 05 '21 at 23:06

1 Answers1

-1

Go slices are references to an underlying array. A slice is like a pointer to a value, not like a direct value. If you use arrays instead of slices, you will see different behavior:

package main
import "fmt"

func main() {
    matrix := [5][5]int{
        {100, 20, 30, 10, 11},
        {15, 100, 16, 4, 2},
        {3, 5, 100, 2, 4},
        {19, 6, 18, 100, 3},
        {16, 4, 7, 16, 100},
    }
    var matrix2 [5][5]int
    matrix2 = matrix
    matrix2[0][1] = 11111111
    fmt.Println(matrix)
    fmt.Println(matrix2)
}

Output:

[[100 20 30 10 11] [15 100 16 4 2] [3 5 100 2 4] [19 6 18 100 3] [16 4 7 16 100]]
[[100 11111111 30 10 11] [15 100 16 4 2] [3 5 100 2 4] [19 6 18 100 3] [16 4 7 16 100]]

You can see that because I used an array [5][5]int of arrays [5]int instead of a slice [][]int of slices []int, the assignment matrix2 = matrix made a copy of the entire matrix.

If you want to use a variable-size matrix, you will have to write your own function to create matrix copies. The above code only works if you know the size of your matrix at compile-time (e.g. 5x5).

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    the wording is not right. There is no such thing like references in the language. quoting https://go.dev/blog/slices-intro `A slice is a descriptor of an array segment. It consists of a pointer to the array, the length of the segment, and its capacity` and `arrays are values` –  Sep 05 '21 at 23:14
  • Thank you so much. This answers my question. Is slice the only type that is like a pointer to a value or is there other types that acts like this? – Leo Sep 05 '21 at 23:18
  • @mh-cbon: A pointer is a reference to a location in memory. – Dietrich Epp Sep 05 '21 at 23:19
  • @mh-cbon: Carefully distinguishing between "pointer" and "reference" is useful in languages like C++, where the terms refer to distinct types. In most other languages, like Java, Python, or Go, it is not so necessary to distinguish "pointer" and "reference". – Dietrich Epp Sep 05 '21 at 23:22
  • 1
    yes, but this specific word is ambiguous. This is not justified, the logic of slice internals perfectly fits the language and using its vocabulary to explain their behavior is more adequate. –  Sep 05 '21 at 23:22
  • @mh-cbon: That is incorrect. There is no ambiguity here, because "reference" doesn't have some distinct meaning in Go. I used "reference" because it is clearer in this case. – Dietrich Epp Sep 05 '21 at 23:23
  • I appreciate any attempts to help make the answer clearer, but this line of discussion doesn't help make the answer clearer. – Dietrich Epp Sep 05 '21 at 23:24
  • Yes, i understand. For the careful reader this should be clear by now. –  Sep 05 '21 at 23:25