On Sun, Mar 4, 2018 at 5:57 AM, Bruno Novais <bruno.nov...@synng.com> wrote: > > As you probably noticed by my question, I'm new to this awesome language > called Go (coming from C/C++). In C++ I rely a lot on constructor (copy) > elision. I think Go doesn't have this concept, but I would like to know what > happen when I do something like this: > > func NewStuff() Stuff { > return Sutff{} > } > > What will happen in that case? Will it be store on the stack, and then > copied to the caller function? That will probably be inlined, so: > > func NewStuff() Stuff { > // A lot of pre-processing that won't be inlined. > > return Sutff{ > Value: var1, > Another: var2, > Etc: var3, > } > } > > The returned value will be copied, or the compiler will optimize and change > the memory space of the caller function?
Go doesn't provide any special guarantees about this case. Different implementations can do different things. That said, in general I believe that a returned struct will be allocated in the caller and changed directly from the callee. Ian -- 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.