On Thu, Jun 25, 2020 at 5:58 PM <chandrak13...@gmail.com> wrote:
>
> Hi,  I am trying to learn Go (I have been working with C++ for a while). I 
> see inconsistency of slices and append
>
>
> func main() {
>
> // 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..

Please see https://blog.golang.org/slices.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWXwK-C9O6srX0aVnqz0RP_%2BQkeOLeh8JY5xZ%2BzS1t1RQ%40mail.gmail.com.

Reply via email to