Golang JSON Marshal / Unmarshal postgres now()

I'm using postgres' now() as a default for my created timestamp, which is generates this:

 id | user_id | title | slug | content |          created           
----+---------+-------+------+---------+----------------------------
  1 |       1 | Foo   | foo  | bar     | 2014-12-16 19:41:31.428883
  2 |       1 | Bar   | bar  | whiz    | 2014-12-17 02:03:31.566419

I tried to use json.Marshal and json.Unmarshal and ended up getting this error:

parsing time ""2014-12-16 19:41:31.428883"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 19:41:31.428883"" as "T"

So I decided to try and create a custom time, but can't seem to get anything working.

Post.go

package models

type Post struct {
    Id      int    `json:"id"`
    UserId  int    `json:"user_id"`
    Title   string `json:"title"`
    Slug    string `json:"slug"`
    Content string `json:"content"`
    Created Tick   `json:"created"`
    User    User   `json:"user"`
}

Tick.go

package models

import (
    "fmt"
    "time"
)

type Tick struct {
    time.Time
}

var format = "2006-01-02T15:04:05.999999-07:00"

func (t *Tick) MarshalJSON() ([]byte, error) {
    return []byte(t.Time.Format(format)), nil
}

func (t *Tick) UnmarshalJSON(b []byte) (err error) {
    b = b[1 : len(b)-1]
    t.Time, err = time.Parse(format, string(b))
    return
}

Any help would be much appreciated, running what I've wrote here gives me this:

json: error calling MarshalJSON for type models.Tick: invalid character '0' after top-level value

it's seems like you're using the wrong format. Postgres uses RFC 3339, which is already defined in the time package. This should work:

time.Parse(time.RFC3339, string(b))

JSON requires strings to be quoted (and in JSON a date is a string), however your MarshalJSON function returns an unquoted string.

I've slightly amended your code and it works fine now:

package models

import (
    "fmt"
    "time"
)

type Tick struct {
    time.Time
}

var format = "2006-01-02T15:04:05.999999-07:00"

func (t *Tick) MarshalJSON() ([]byte, error) {
    // using `append` to avoid string concatenation
    b := make([]byte, 0, len(format)+2)
    b = append(b, '"')
    b = append(b, t.Time.Format(format)...)
    b = append(b, '"')
    return b, nil
}

func (t *Tick) UnmarshalJSON(b []byte) (err error) {
    b = b[1 : len(b)-1]
    t.Time, err = time.Parse(format, string(b))
    return
}