[go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-14 Thread xiiophen
Slightly better example to above https://play.golang.org/p/eGCw4ldGiZ both pointer checks fail, but initially the underlying arrays are the same, not so after the append. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from thi

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-14 Thread xiiophen
On Monday, 13 March 2017 21:14:27 UTC, Dave Cheney wrote: > > A few clarifications, b does not point to a. a and b are variables of type > []int. The value of each slice's ptr fields point to a backing array of > some capacity. > > If you append to a, this does not change th length or cap fiel

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-13 Thread Dave Cheney
A few clarifications, b does not point to a. a and b are variables of type []int. The value of each slice's ptr fields point to a backing array of some capacity. If you append to a, this does not change th length or cap fields of b. If you do this enough times that ptr field of a will be updat

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-13 Thread xiiophen
On Monday, 13 March 2017 07:20:57 UTC, Jan Mercl wrote: > On Mon, Mar 13, 2017 at 8:05 AM st ov > > wrote: > > > I know it can be accessed the question relates to this > > > http://stackoverflow.com/questions/36706843/how-to-get-the-underlying-array-of-a-slice-in-go > > Let me ask what do

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-13 Thread st ov
just a thought experiment :) On Monday, March 13, 2017 at 12:20:57 AM UTC-7, Jan Mercl wrote: > > On Mon, Mar 13, 2017 at 8:05 AM st ov > > wrote: > > > I know it can be accessed the question relates to this > > > http://stackoverflow.com/questions/36706843/how-to-get-the-underlying-array-of-a-

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-13 Thread Konstantin Khomoutov
On Sun, 12 Mar 2017 17:36:53 -0700 (PDT) st ov 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

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-13 Thread Jan Mercl
On Mon, Mar 13, 2017 at 8:05 AM st ov wrote: > I know it can be accessed the question relates to this > http://stackoverflow.com/questions/36706843/how-to-get-the-underlying-array-of-a-slice-in-go Let me ask what do you need the backing array for which cannot be done using the slice value? Using

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-13 Thread st ov
I know it can be accessed the question relates to this http://stackoverflow.com/questions/36706843/how-to-get-the-underlying-array-of-a-slice-in-go On Sunday, March 12, 2017 at 10:01:11 PM UTC-7, Jan Mercl wrote: > > What is a reference to the array? To access the array use the slice itself > a

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-12 Thread Jan Mercl
What is a reference to the array? To access the array use the slice itself as usual, no reflect needed. On Mon, Mar 13, 2017, 01:37 st ov wrote: > Thanks! > anyway to get a reference to that new array without using reflect? > > > > > On Sunday, March 12, 2017 at 12:13:03 PM UTC-7, Jan Mercl wrot

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-12 Thread st ov
Thanks! anyway to get a reference to that new array without using reflect? On Sunday, March 12, 2017 at 12:13:03 PM UTC-7, Jan Mercl wrote: > > On Sunday, March 12, 2017 at 11:49:33 AM UTC-7, st ov wrote: > > > What happens when appending to a slice exceeds the backing capacity? > > If the capac

Re: [go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-12 Thread Jan Mercl
On Sunday, March 12, 2017 at 11:49:33 AM UTC-7, st ov 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

[go-nuts] Re: What happens when appending to a slice exceeds the backing capacity?

2017-03-12 Thread st ov
pasting code for convenience func main() { // create backing array arr := [5]int {0,1,2,3,4} fmt.Println(arr) // take a slice of backing array s1 := arr[:3] fmt.Println(s1) fmt.Println(arr) fmt.Println(cap(s1)) // append to slice, replaces backing