title: How to detect whether a struct pointer is nil in golang?
body: <p>As is shown by the following code: <code>n := localplugin.NewPluginNet()</code>, the type of <code>n</code> is <code>localnet.Net</code>, which is a interface implemented by the struct pointer <code>*localnet.DomainSocket</code>. Function <code>func NewPluginNet() localnet.Net</code> returns a nil pointer to <code>n</code>.</p>
  
  <pre><code>var n localnet.Net    
  n = localplugin.NewPluginNet()
  fmt.Println("----&gt; ", n)
  if n == nil {
      fmt.Println("n is nil")
  }else{
      fmt.Println("n is not nil : ",  n)
  }
  fmt.Println(reflect.TypeOf(n), reflect.TypeOf(nil), n)
  </code></pre>
  
  <p>The following is the output of the code above.</p>
  
  <pre><code>----&gt;  &lt;nil&gt;
  n is not nil :  &lt;nil&gt;
  *localnet.DomainSocket &lt;nil&gt; &lt;nil&gt;
  </code></pre>
  
  <p>Why does n is not nil ?</p>
  
  <p>===================================</p>
  
  <pre><code>var n1 *localnet.DomainSocket
  n1 = nil
  fmt.Println("&gt;&gt; ", n1)
  if n1 == nil {
      fmt.Println("&gt;&gt;n1 is nil")
  }else{
      fmt.Println("&gt;&gt;n1 is not nil : ",  n)
  }
  fmt.Println(reflect.TypeOf(n1), reflect.TypeOf(nil), n1)
  </code></pre>
  
  <p>However, it's correct for this case:</p>
  
  <pre><code>&gt;&gt;  &lt;nil&gt;
  &gt;&gt;n1 is nil
  *localnet.DomainSocket &lt;nil&gt; &lt;nil&gt;
  </code></pre>