trainer icon indicating copy to clipboard operation
trainer copied to clipboard

Slices question

Open rusinikita opened this issue 10 months ago • 0 comments

Understanding slices work

func main() {
	var x []int
	x = append(x, 0) // explanation 1: new array created with capacity 2
	x = append(x, 1) // explanation 2: same array index 1 set to 1
	x = append(x, 2) // explanation 3: new array created with len 4, 012 setted

        // expected: [0 1 2 3 0 1 2 4]
        // wtf: [0 1 2 4 0 1 2 4]
	fmt.Println(append(append(x, 3), append(x, 4)...))
        // explanation 4: no new arrays created, index 3 set to 3, and then to 4
}

rusinikita avatar Mar 27 '24 19:03 rusinikita