Thanks Roberto. 

That certainly explains the second result, so it perhaps updates my 
"expected" result to be the second and not the first.

Unfortunately it does not necessarily explain why the first result acts 
differently. 

I can reproduce what you describe in go playground, but not the varying 
results I'm seeing.

https://play.golang.org/p/g6GXCW1aq8

I'm sure there are some finer points in a more complex system/example about 
when it would and would not be true that the capacity is there to not 
reallocate, and that is probably the interaction I'm dealing with.

I don't necessarily need to have that question answered (although I would 
like to know!), as the solution will be the same regardless. If I need to 
always ensure that this will be a different array underneath, I need to 
write that code explicitly.

Thank you,

Evan







On Friday, 15 July 2016 10:37:17 UTC-7, Roberto Zanotto wrote:
>
> https://golang.org/pkg/builtin/#append
>
> If the initial slice ns has enough capacity, it will not be reallocated, 
> but only resliced and modified in place.
> So, in your second example, ns1 and ns2 can end up being two slices that 
> share the same underlying array.
>
> On Friday, July 15, 2016 at 7:28:32 PM UTC+2, Evan Digby wrote:
>>
>> I can't reproduce this in go playground (yet), but under what 
>> circumstances would/could/should:
>>
>> nss := []namespace.Namespace{
>> append(ns, g2message.NamespaceWin...),
>> append(ns, g2message.NamespaceSpend...),
>> }
>>
>> Act differently than:
>>
>> ns1 := append(ns, g2message.NamespaceWin...)
>> ns2 := append(ns, g2message.NamespaceSpend...)
>>
>> nss := []namespace.Namespace{
>> ns1,
>> ns2,
>> }
>>
>> ns is a parameter on the function (actually, it's a parameter on the 
>> function that generated the function that is being called, and ns is a 
>> closure), and is currently: 
>>
>> namespace.New("localhost")
>>
>> The definitions in the "namespace" and "g2message" packages are:
>>
>> type Namespace []string
>>
>> var NamespaceWin namespace.New("win") // New is making Namespace{"win"}
>> var NamespaceSpend namespace.New("spend")
>>
>> I would think under no circumstances; however, that's not my experience. 
>>
>> Result of fmt.Println(nss) on the first example (expected):
>>
>> [[localhost win] [localhost spend]]
>>
>> Result of fmt.Println(nss) on the second example:
>>
>> [[localhost spend] [localhost spend]]
>>
>>
>> It's not running in a loop, separate goroutine, or anything. Those lines 
>> are all that change between the two results. I'm at a loss. I will keep 
>> trying to reproduce in go playground.
>>
>>
>> Thanks,
>>
>>
>> Evan
>>
>

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