Problem with slicing the slice

Hi,

I am trying to learn go and wrote a very simple piece of code here but I don’t understand why I am getting wrong output for “a” and “b” variables.

Can anybody explain?

You need to be aware :
- if there is enough capacity, the underlying array is reused;
- if not, a new underlying array is allocated and the data is copied over.

In your sample, there is enought space to do the append, so it is done in the same underlying array and then change the values of a and b.

1 Like

I agree with @yamil_brancho, check this code https://play.golang.org/p/CGzLyNCqAxUs.

In this example, the variable c contains a new array in another memory position.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.