// append example within capacity
var m []int = []int{1, 2, 3}
a := m[0:2]
b := append(a, 4)
a[0] = -1
fmt.Printf("%v, %d, %d\n", m, len(m), cap(m))
fmt.Printf("%v, %d, %d\n", a, len(a), cap(a))
fmt.Printf("%v, %d, %d\n", b, len(b), cap(b))
// append example with more than capacity
var m1 []int = []int{1, 2, 3}
a1 := m1[0:2]
b1 := append(a1, 4, 5)
a1[0] = -1
fmt.Printf("%v, %d, %d\n", m1, len(m1), cap(m1))
fmt.Printf("%v, %d, %d\n", a1, len(a1), cap(a1))
fmt.Printf("%v, %d, %d\n", b1, len(b1), cap(b1))
}
output is
--------
[-1 2 4], 3, 3
[-1 2], 2, 3
[-1 2 4], 3, 3
[-1 2 3], 3, 3
[-1 2], 2, 3
[1 2 4 5], 4, 6
Essentially based on the existing capacity, the assignment of one slice effects other slices. These are stemming from the underlying pointer arithmetic and seems inconsistent. Looks like programmer needs to know the history of capacity before understanding the ramifications of slice assignments.
Excuse me if this is basic question. Thought to ask..
Regards
Ck