ardanlabs / gotraining

Go Training Class Material :
http://www.ArdanLabs.com
Other
11.87k stars 2.17k forks source link

Sharing variable up the call stack #364

Closed seinshah closed 2 years ago

seinshah commented 2 years ago

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

ardan-bkennedy commented 2 years ago

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.