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 array?
    s1 = append(s1, 7)
    fmt.Println(s1)
    fmt.Println(arr)
    
    // append to slice, doesn't replace backing array?
    // whats backing the slice?
    s1 = append(s1,8,9,10)
    fmt.Println(s1)
    fmt.Println(arr)
    fmt.Println(cap(s1))
}




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?
>
> https://play.golang.org/p/mVezWL4Cbe
>
> In the example given, appending within capacity on line 20 modifies the 
> backing array at the successive index.
> But on line 26, when appending exceeds the capacity, the backing array is 
> no longer modified. So where are those additional elements stored?
>
> Also the capacity for this new backing store is 12, this is to amortize 
> performance correct?
>

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