Example (go playgrounds)
package main
import (
"fmt"
)
type A struct {
B *B
}
type B struct {
C *C
}
type C struct {
D string
}
func main() {
a := A{}
// I'd like to avoid this repetitive statements
if a.B != nil && a.B.C != nil && a.B.C.D != "" {
fmt.Println("no nil-pointer panic here")
}
// I'd like to go into that if body if any of these are nil and D is not empty string
if a.B.C.D != "" {
fmt.Println("nil-pointer panic here")
}
}
I can't figure out a way to check if a property is empty in a chain of pointers which should also result into false
if any of them is nil. Assume these fields have a bit of a longer name - and the struct is a bit more complex - this gets unreadable pretty fast. But maybe there's no other way...