title: How does one avoid "invalid memory address or null pointer dereference" errors?
body: <p>I'm wondering how I can structure this example code to help avoid null pointer dereference panics:</p>
  
  <pre><code>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, &amp;astructlist} // line 6
  
      for _, x := range(*c.Bstruct.AStructList) {
          fmt.Printf("%s\n", &amp;x)
      }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks in advance for any help!</p>