Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-06 Thread Christoph Berger
This describes pretty clearly what happens behind the scenes. Still, someone could wonder why append then does not receive a pointer to the slice, to ensure that both the slice header and the slice data are treated in a consistent way. What is the generally accepted answer to this? For exampl

Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-05 Thread Dave Cheney
I think #1 would be clearer if it were written as 1) "We must return the slice afterwards because ... the our slice value (the run-time data structure holding the pointer, length, and capacity) is a copy of the callers.” -- You received this message because you are subscribed to the Google Gro

Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-05 Thread Tamás Gulácsi
Both reasons correct: the underlying array, length and cap may change, and as all arguments are pasded by value, we can't see the change if we don't use (assign) the returned value. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe

Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-05 Thread Ben Hoyt
> > I don't follow. It had been shown, that the result of append is actually > not needed/can be constructed by other means - except in the case where the > backing array gets reallocated. Then you can't get away without it. > Hmmm, it feels like we're talking past each other. Of course you can ig

Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-05 Thread Jan Mercl
On Sun, Nov 5, 2017 at 3:16 AM Ben Hoyt wrote: > Hence the confusion. Sure, append can write to the underlying array because that's effectively passed by reference. But it can't update the length of the slice itself, unless you return it. I don't follow. It had been shown, that the result of app

Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-04 Thread Ben Hoyt
Hmmm, but in that first link you're updating the slice manually after the append() -- that's exactly my point. The append() call can't touch the caller's slice value (ptr, len, cap) because it's passed by value. So in the code you wrote: s := make([]int, 0, 10) for i := 0; i < 10; i++ { _ = append

Re: [go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-04 Thread Jan Mercl
On Sat, Nov 4, 2017 at 1:53 PM Ben Hoyt wrote: > It seems to me that the reasons given for why you need to use the return value of append() are confusing and not quite right. For example, at https://blog.golang.org/slices#TOC_9. Rob Pike says: > > "In this case it's especially important to return

[go-nuts] Rationale in docs on why you need to use the result of append()

2017-11-04 Thread Ben Hoyt
Hi folks, It seems to me that the reasons given for why you need to use the return value of append() are confusing and not quite right. For example, at https://blog.golang.org/slices#TOC_9. Rob Pike says: "In this case it's especially important to return the slice, since when it reallocates th