4 ms·
Absolutely agreed on the ridding of constructors. Offer the option to set things (eg the Logger), and if it's nil, use some defaults. This seems much cleaner
by axaxs 9y ago
Absolutely agreed on the ridding of constructors. Offer the option to set things (eg the Logger), and if it's nil, use some defaults. This seems much cleaner than making a huge function signature. The net/http package does this really well.
- ninkendo 9y agoAre you going to do the nil check in every member function though? I run into this problem with Go a lot, actually. (I'd like to make initialization of a some member variable optional, but checking if it's nil every time I use it becomes a pain. Not all types have useful zero values either... I'm looking at you, map.)
- axaxs 9y agoWell, in my cases I usually add an unexported 'check()' method, which is generally called by every other method that needs state. It would basically just be something of x.Z == nil {x.Z = DEFAULTZ}