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, 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) // we have 1 element > x[0] = 42 > for i := 0; i < 4; i++ { > > z := x > > z[0] = z[0] + 1 > > z = append(z, i)) > > } > > fmt.Println(x[0]) // would print 46 > fmt.Println(len(x) // still 1 > > > Well, unless you move z's declaration out of the loop as Jan suggested. > > I'd recommend reading > https://blog.golang.org/go-slices-usage-and-internals (manipulating slice > values was not obvious to me until I read this). > > Le jeudi 2 mars 2017 15:23:21 UTC+1, James Bardin a écrit : >> >> 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 wrote: >>> >>> 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 2, 2017 at 3:56:58 PM UTC+2, Jan Mercl wrote: >>>> >>>> On Thu, Mar 2, 2017 at 2:46 PM <bar...@gmail.com> 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, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.