I’m wondering how I can structure this example code to help avoid null pointer dereference panics:
package main
import "fmt"
type Astruct struct {
Number int
Letter string
}
type Bstruct struct {
foo int
AStructList *[]Astruct
}
type Cstruct struct {
Bstruct
}
func (a *Astruct) String() string {
return fmt.Sprintf("Number = %d, Letter = %s", a.Number, a.Letter)
}
func main() {
astructlist := make([]Astruct, 3) // line 1
for i := range astructlist { // line 2
astructlist[i] = Astruct{i, "a"} // line 3
} // line 4
c := new(Cstruct)
c.Bstruct = Bstruct{100, &astructlist} // line 6
for _, x := range(*c.Bstruct.AStructList) {
fmt.Printf("%s\n", &x)
}
}
If I omit lines 1-4 and 6 of main(), I get a null pointer dereference panic. Short of checking if c != nil, is there a way to avoid these panics?
Thanks in advance for any help!
In this particular case, you could use idiomatic Go. Change
AStructList *[]AstructtoAStructList []*Astruct. For example,In general, it’s your responsibility to either assign a non-
nilvalue to a pointer or test fornilbefore its use. When you allocate memory without explicitly intializing it, it’s set to the zero value for the type, which isnilfor pointers.