title: Initialize a nil pointer struct in method
body: <p>I have a struct called <code>Article</code> which has a field called <code>Image</code>. Per default <code>Image</code> has value <code>nil</code>. As <code>Image</code> should be only persisted as <code>Image.Id</code> to database I use the <code>bson.BSONGetter</code>, <code>bson.BSONSetter</code> and <code>json.Marshaler</code> interfaces to fake this behavior.</p>
  
  <p>However internally it is possible to use <code>Image</code> as an <code>io.ReadWriteCloser</code> if I load a file onto this with some other helper.</p>
  
  <pre><code>package main
  
  import (
      "io"
      "fmt"
  
      "gopkg.in/mgo.v2"
  )
  
  type Article struct {
      Name  string
      Image *Image
  }
  
  type Image struct {
      Id interface{}
  
      io.ReadWriteCloser
  }
  
  func (i *Image) SetBSON(r bson.Raw) error {
      i = &amp;Image{}
  
      return r.Marshal(i.Id)
  }
  
  func (i *Image) GetBSON() (interface{}, error) {
      return i.Id
  }
  
  func (i *Image) MarshalJSON() ([]byte, error) {
      return json.Marshal(i.Id)
  }
  </code></pre>
  
  <p><a href="http://play.golang.org/p/WDnaHlQBuU" rel="nofollow">Playground</a></p>
  
  <p>The problem with this approach now is that it is not possible to initialize <code>Image</code> in <code>Image.SetBSON</code> as <code>Image</code> is <code>nil</code>.</p>