On Fri, Jul 31, 2020 at 8:47 AM ____ <
karath...@gmail.com> wrote:
>
> I would appreciate it if someone could help me understand what exactly I'm doing in the code below. My experience with Go spans a few weekends in total so please bear with the naiveté of my question.
>
> When I do this
>
>
https://play.golang.org/p/rQvmZEqq8tp
>
> the factory and the gadget share the same specs. When I do this
>
>
https://play.golang.org/p/-Gi5N-kpKFq
>
> they don't.
>
> Question: What is the proper way to reason about this code?
>
> I'm mostly looking for an efficient mental model that makes an expert go "of course...", because I've been trying to track references and pointers in my head with no success.
A slice is a struct that contains a pointer to an underlying array.
When you change a slice, you change the pointer. When you change an
element of a slice, you change an element of the underlying array.
It may help to read
https://blog.golang.org/slices-intro.
Ian