I'm still new to Go and am facing a situation that needs some help here.
Assume there are two types of structs with the SAME attribute list:
type PersonA struct {
[long list of attributes...]
}
type PersonB struct {
[same long list of attributes...]
}
And I would like to create an instance and initialize based on some conditions like below:
var smartPerson [type]
func smartAction(personType string) {
switch personType
case "A":
smartPerson = PersonA{long list initialization}
case "B":
smartPerson = PersonB{same long list initialization}
}
fmt.Println(smartPerson)
There are two problems here:
First - 'smartPerson' needs to be the instance type ascertained in the switch statement.
Second - the 'long list initialization' is the same for all conditions, so better to avoid repetition.
Is it possible to do this in Go?
PersonAandPersonBembed that.