title: Timestamps in Golang
body: <p>Trying to get this approach to timestamps working in my application: <a href="https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go" rel="nofollow">https://gist.github.com/bsphere/8369aca6dde3e7b4392c#file-timestamp-go</a></p>
  
  <p>Here it is:</p>
  
  <pre><code>package timestamp
  
  import (
      "fmt"
      "labix.org/v2/mgo/bson"
      "strconv"
      "time"
  )
  
  type Timestamp time.Time
  
  func (t *Timestamp) MarshalJSON() ([]byte, error) {
      ts := time.Time(*t).Unix()
      stamp := fmt.Sprint(ts)
  
      return []byte(stamp), nil
  }
  
  func (t *Timestamp) UnmarshalJSON(b []byte) error {
      ts, err := strconv.Atoi(string(b))
      if err != nil {
          return err
      }
  
      *t = Timestamp(time.Unix(int64(ts), 0))
  
      return nil
  }
  
  func (t Timestamp) GetBSON() (interface{}, error) {
      if time.Time(*t).IsZero() {
          return nil, nil
      }
  
      return time.Time(*t), nil
  }
  
  func (t *Timestamp) SetBSON(raw bson.Raw) error {
      var tm time.Time
  
      if err := raw.Unmarshal(&amp;tm); err != nil {
          return err
      }
  
      *t = Timestamp(tm)
  
      return nil
  }
  
  func (t *Timestamp) String() string {
      return time.Time(*t).String()
  }
  </code></pre>
  
  <p>and the article that goes with it: <a href="https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f" rel="nofollow">https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f</a></p>
  
  <p>However, I'm getting the following error:</p>
  
  <pre><code>core/timestamp/timestamp.go:31: invalid indirect of t (type Timestamp)                                                                                                                                                     
  core/timestamp/timestamp.go:35: invalid indirect of t (type Timestamp)
  </code></pre>
  
  <p>My relevant code looks like this:</p>
  
  <pre><code>import (
      "github.com/path/to/timestamp"
  )
  
  type User struct {
      Name        string
      Created_at  *timestamp.Timestamp  `bson:"created_at,omitempty" json:"created_at,omitempty"`
  } 
  </code></pre>
  
  <p>Can anyone see what I'm doing wrong?</p>
  
  <p><strong>Related question</strong>
  I can't see how to implement this package either. Do I create a new User model something like this?</p>
  
  <pre><code>u := User{Name: "Joe Bloggs", Created_at: timestamp.Timestamp(time.Now())}
  </code></pre>