Consider the following minimal example:
package main
import "fmt"
type runner interface {
s1(int)
s2(int)
}
type test struct {
x1 []int
x2 []int
}
func (t test) s1(v int) {
t.x1 = append(t.x1, v)
t.s2(v)
}
func (t test) s2(v int) {
t.x2[v] = v
}
func main() {
t := test{
x1: make([]int, 0),
x2: make([]int, 10)}
for i := 0; i < 10; i++ {
t.s1(i)
}
fmt.Println(t)
}
Now if you run it, you will get a result like this:
{[] [0 1 2 3 4 5 6 7 8 9]}
meaning that the x1
array is never populated. Or actually, it is, but resets on each time s1
function exits. s2
works just fine placing items in a pre-defined array.
Does anyone know what exactly is going on here? Is it because of the scope of array amends? It seems a little counter intuitive.
P.S. I do understand that x1 is a slice, where x2 is an actual pre-defined array. My own theory goes that if you work with "slices", they can only be changed within a specific scope, not anywhere else.