Struct Initial Values
avivcarmis opened this issue · 0 comments
avivcarmis commented
What's the best approach for us to provide struct initial values:
- can we support hard coded values?
- can we support calculated values? can we use values of other fields?
- can we accept values as arguments?
require accepting values is problematic because it will break backward compatibility of no argument initialization in zero value of structs, reflect.New
and other use cases. can we work around it somehow?
I had something like this in mind:
private type Foo struct {
private fieldA string // zero value
private fieldB string = 1 // hard coded value
private fieldC string = self.calculateFieldC() // calculated value
}
func (f *Foo) calculateFieldC() string {
return f.fieldB * 3
}
- what can we do about this self?
- does it answer all requirements
More context in this blog post section.