But this is only working as long as you do not need any services or 
dependencies. And in that case i would really appreciate if the language 
natively supports a nil check on method parameters at compile time.
For all cases where nil is a valid value, the language needs a marker to 
allow such cases. In my opinion it would save some nil checks and makes it 
much more clear for the caller what the method is expecting.



Am Sonntag, 4. März 2018 04:20:11 UTC+1 schrieb Dave Cheney:
>
> 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