emahiro/b.log

Drastically Repeat Yourself !!!!

MySQL Driver で time.Time 型をパースする

以下のような Struct を考える。

type User struct {
    ID int64
    Name String
    CreatedAt time.Time
    UpdatedAt time.Time
}

time.Time の時刻型をもつフィールドがある時、MySQL の時刻型のカラムをそのまま Scan しようとすると以下のエラーが出る。 sql: Scan error on column index 3: unsupported Scan, storing driver.Value type []uint8 into type *time.Time

解決策としては接続する時に ?parseTime=true をつける。

https://stackoverflow.com/questions/45040319/unsupported-scan-storing-driver-value-type-uint8-into-type-time-time が参考になる。