Golang Slice Append Return

slice2 := [][]int{}
   for i := range slice {
       slice := []int{0, 0}
       slice[0] = i
       slice[1] = i
       slice2 = append(slice2, slice)

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 (the maximum length of the segment).
Here appending a slice(pointer) not the slice’s values. To get the desired result you have to declare a new slice in each iteration and append the new slice to slice2.

Subscribe for
weekly updates