4 ms·
That code is broken; the observation that a field has its default value does not imply that no value was set. Test case: func main() { f(&Config{a: 0, b
by Someone 13y ago
That code is broken; the observation that a field has its default value does not imply that no value was set.
Test case:
func main() {
f(&Config{a: 0, b:"", c:0.0, d:nil})
}
That calls f(11,"some default",55.5,D())
=> It is even harder to replicate that Python idiom (I wouldn't know how, but I have only glanced at the go language spec. It would help if you could auto-initialize structure members with values only known the the structure itself, or if one could replace that &Config above by a function call, like this: makeConfig(){c: 44.4})
- fauigerzigerk 13y ago"That code is broken; the observation that a field has its default value does not imply that no value was set" That doesn't necessarily mean the code is broken. It may or may not be possible in a particular situation to treat the zero value as semantically special as I have done. But if it's not, then you are of course right that this creates an additional problem. Using a seperate function to initialize a Config is fine, but you would actually have to create a makeConfigForF function because the defaults are specific to f and not to Config. It's all messy. That's why I like keyword default arguments.