title: How to assign to a field of nil struct in golang
body: <p>I'm trying to assign a value to a field, but my program panics with <code>runtime error: invalid memory address or nil pointer dereference</code>.</p>
  
  <pre><code>package main
  
  type Node struct {
      Value int
  }
  
  func (n *Node) SetValue(value int) {
      n.Value = value
  }
  
  func main() {
      var n *Node
  
      n.SetValue(1)
  }
  </code></pre>
  
  <p>This is reasonable since variable is nil.<br/>
  But I've fount some Go internal structs are allowed to do this, e.g. <code>bytes.Buffer</code></p>
  
  <pre><code>package main
  
  import "bytes"
  import "io"
  import "os"
  
  func main() {
      var b bytes.Buffer
      b.Write([]byte("Hello world"))
      io.Copy(os.Stdout, &amp;b)
  }
  </code></pre>
  
  <p>Here is the `bytes.Buffer source code</p>
  
  <pre><code>func (b *Buffer) Write(p []byte) (n int, err error) {
      b.lastRead = opInvalid
      m := b.grow(len(p))
      return copy(b.buf[m:], p), nil
  }
  </code></pre>
  
  <p>Is it the thing only builtin structs can do or it's possible to accomplish this in my code?</p>
  
  <p><strong>EDIT</strong><br/>
  Here is the working example. Thanks @twotwotwo for suggestion.</p>
  
  <pre><code>package main
  
  import "fmt"
  
  type Node struct {
      Value int
  }
  
  func (n *Node) SetValue(value int) {
      n.Value = value
  }
  
  func main() {
      var n Node
  
      n.SetValue(1)
  
      fmt.Println(n.Value)
  }
  </code></pre>