ardanlabs / gotraining

Go Training Class Material :

Home Page:http://www.ArdanLabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sharing variable up the call stack

seinshah opened this issue · comments

@ardan-bkennedy, thank you for this amazing course and repository organization.

During decoupling and in interface pollution topic, you have fixed the pollution, but the New function is sharing the concrete value of the type up the call stack.

composition/README.md#L63 - Playground Example

I know this is a very popular design, but I can't understand why do we need to allocate memory here and share the value instead of returning a copy of the value?

Do we prefer that the caller will have easier access to methods with pointer receiver if we share the value up the call stack over memory allocation and the risk of memory leak?

Complier optimizations will eliminate the allocation. The question is really which data semantic is best for the API. In the class and book and spend a lot of time trying to provide information to help answer that question.