On Sun, 12 Mar 2017 17:36:53 -0700 (PDT) st ov <[email protected]> wrote:
> > > What happens when appending to a slice exceeds the backing > > > capacity? > > > > If the capacity of s is not large enough to fit the additional > > values, append allocates a new, sufficiently large underlying array > > that fits both the existing slice elements and the additional > > values. Otherwise, append re-uses the underlying array. > > > > src: https://golang.org/ref/spec#Appending_and_copying_slices > > > anyway to get a reference to that new array without using reflect? But this kind of "reverses" the whole idea of a slice which is a view into the underlying array -- into any location within its data span. If you need to have something like this, have a "master" slice referencing the whole array from its beginning, and always use this slice for appending. The address of the backing array is then the address of the first slice's element. That is, s := make([]byte, 100) s = append(s, 100) p := &s[0] // The address of the backing array. Note that if you have other slices pointing to the same array such a master slice uses, you'll need to "reslice" them after appending to the master slice. -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
