type Human struct {
Name string
MaybeHasCat *Cat
}
type Cat struct {
Name string
}
func main() {
h := Human{}
h.MaybeHasCat.Name = "Taffy"
}
And boom. Null pointer exception because MaybeHasCat is null. The fact that go doesn't let you define an optional type is such a pain. A lot of newcomers while getting trained up fall for this bug.edit: updating to add the pointer i missed. also adding a playground link: https://go.dev/play/p/izcod8xF7ZQ