On Sat, Nov 4, 2017 at 1:53 PM Ben Hoyt <benh...@gmail.com> 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 the slice, since when
it reallocates the resulting slice describes a completely different array."
>
>
> But (if I understand correctly) you need to return the slice *every*
time, not because the slice reallocates, but because the slice is passed by
value, so no changes
> to the length will be reflected to the caller unless you return the new
slice. It doesn't matter whether or not the underlying array has changed or
been reallocated.

Consider: https://play.golang.org/p/fMiZhQCVIt. It works perfectly while
ignoring the result of append. If you s/10/11/ at line 9, it will fail.
Compare to https://play.golang.org/p/Ohl8e6DmZy where the result of append
is not ignored and it works for 11 elements just fine.

So technically you don't need the result of append as long as the
underlying array does not get reallocated and the cap is still sufficient
(the same fact again, actually). IOW, Rob's quote above is correct and I'm
not sure what confusion you do perceive about this.

-- 

-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.

Reply via email to