>
> I prefer the later when possible because it enables callers to use the 
> zero value of a type without explicit initialisation.


Two great standard library examples of this are sync.Mutex / sync.RWMutex 
and bytes.Buffer / strings.Builder.

Matt

On Saturday, March 3, 2018 at 9:20:11 PM UTC-6, Dave Cheney wrote:
>
> I prefer the later when possible because it enables callers to use the 
> zero value of a type without explicit initialisation.
>
> On Sunday, 4 March 2018 11:37:43 UTC+11, Anmol Sethi wrote:
>>
>> How do you guys choose between constructors and lazy initialization?
>>
>> For example.
>>
>> Struct constructors:
>>
>> type meow struct {
>>     x http.Handler
>> }
>>
>> func newMeow() *meow {
>>     return &meow{
>>         x: http.NewServeMux(),
>>     }
>> }
>>
>> func (m *meow) do() {
>>     // stuff
>> }
>>
>>
>> Lazy initialization:
>>
>> type meow struct {
>>     x http.Handler
>> }
>>
>> func (m *meow) do() {
>>     if m.x == nil {
>>         m.x = http.NewServeMux()
>>     }
>>     // stuff
>> }
>>
>>

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