I don t know if there following if possible. I have:
type A struct {
Val int
}
以及
type B struct {
Val *int
}
接着,我有这一努力。
func main() {
b := B{Val: new(int)}
*b.Val = 1
a := A{Val: *b.Val}
}
What I want to achieve is that the variable a
gets initialized with its zero values, hence its Val
being 0
in case *b.Val = nil
without having to check with an if
condition. This is a very simplified version 以及 the reason I am trying to do this is that my B
struct has many properties that might or might not be filled by different entities. Such properties make more sense as nil
in case they are not filled 以及 I am trying to avoid checking with an if
condition for each one of them if they are nil
before assigning their non-nil value or the zero value to the properties of A