title: Marshalling time.Time to unix timestamp
body: <p>I have a struct like so </p>
  
  <pre><code>type log struct {
      [...]
      Timestamp timestamp `json:"timestamp"`
  }
  </code></pre>
  
  <p>and I want to have the Timestamp as unix timestamp instead of whatever go does by default (<code>2018-09-21T19:31:03.291Z</code>) </p>
  
  <p>I've tried creating a custom type for that like this:</p>
  
  <pre><code>type timestamp struct {
      time.Time
  }
  
  func (t timestamp) MarshalJSON() ([]byte, error) {
      return []byte(strconv.FormatInt(time.Time(t.Time).Unix(), 10)), nil
  }
  
  func (t *timestamp) UnmarshalJSON(data []byte) error {
      i, err := strconv.ParseInt(string(data[:]), 10, 64)
      if err != nil {
          return err
      }
      t = &amp;timestamp{
          time.Unix(i, 0),
      }
      return nil
  }
  </code></pre>
  
  <p>but I'm always getting the error <code>can not unmarshal timestamp into *main.timestamp</code> when trying to read from the database. </p>
  
  <pre><code>for iter.Scan(&amp;message.Text, &amp;message.Timestamp) {
  
      userlogResult.Messages = append(userlogResult.Messages, message)
  }
  if err := iter.Close(); err != nil {
      log.Error(err)
  }
  </code></pre>
  
  <p>It can't unmarshall the Timestamp here. <a href="https://github.com/gocql/gocql/blob/799fb0373110eaa4e2e71dd32a9b5903f80dca8f/helpers.go#L30" rel="nofollow noreferrer">https://github.com/gocql/gocql/blob/799fb0373110eaa4e2e71dd32a9b5903f80dca8f/helpers.go#L30</a> the issue is that it doesn't use the Unmarshall functions.</p>
  
  <p>Edit: I've answered my own question.</p>