Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-14 Thread db047h
Internally, a slice variable is a SliceHeader struct (see https://golang.org/pkg/reflect/#SliceHeader), NOT a pointer to it. Note that len and cap are properties of the SliceHeader struct, NOT of the backing array. So yes, I made a mistake, the x slice header is copied into z at every iteratio

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-14 Thread bart2yk
And more, if you assign a slice to another at the beginning will have the same pointer, until one of them will change the capacity. https://play.golang.org/p/BWG5tqayj9 On Monday, March 6, 2017 at 12:12:10 AM UTC+2, db0...@gmail.com wrote: > > This happens only because z is copied every time, bu

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-14 Thread bart2yk
example: https://play.golang.org/p/d9KSHScDbL On Monday, March 6, 2017 at 12:12:10 AM UTC+2, db0...@gmail.com wrote: > > This happens only because z is copied every time, but has nothing to do > with interfaces. This exhibits the same behavior without interface{}: > https://play.golang.org/p/C5K

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-14 Thread bart2yk
z isn't copied, append will return another pointer only if the capacity will change, but the slice was created from the beginning with enough cap. On Monday, March 6, 2017 at 12:12:10 AM UTC+2, db0...@gmail.com wrote: > > This happens only because z is copied every time, but has nothing to do >

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-05 Thread db047h
This happens only because z is copied every time, but has nothing to do with interfaces. This exhibits the same behavior without interface{}: https://play.golang.org/p/C5KoUST4Zn You could in fact modify the slice's existing content, but not the slice itself. For example: x := make([]int, 1, 5

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-02 Thread James Bardin
All assignments are a copy, so there's no way to modify a slice value that is in an interface{} Just like if this were a function argument, you will need a pointer in order to modify the value. https://play.golang.org/p/kOdXUCiT_F On Thursday, March 2, 2017 at 9:01:48 AM UTC-5, Cornel Damian

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-02 Thread bart2yk
If you look where the slice is printed you will see that the data is missing. In the working example from me the data is there. And even so, your example doesn't help me, what i've set there is just for example, i my code each time i add data to the slice i must first cast it. On Thursday, March

Re: [go-nuts] Slice from interface doesn't work as expected

2017-03-02 Thread Jan Mercl
On Thu, Mar 2, 2017 at 2:46 PM wrote: > Can somebody explain me why? Did you mean https://play.golang.org/p/GUVZBUQlEj ? -- -j -- 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,