Here I saw a question about nullable fields in struct.
From Go 1.13 you can use sql.NullTime (earlier declared in pg
and mysql
).
From now we can use these two options to declare the ResolvedAt
nullable time field:
A. ResolvedAt as NullTime:
type Event struct {
Id int
Resolved bool
CreatedAt time.Time
ResolvedAt sql.NullTime
}
B. ResolvedAt as pointer:
type Event struct {
Id int
Resolved bool
CreatedAt time.Time
ResolvedAt *time.Time
}
For Scan
method there is no difference:
rows, err := db.Query("SELECT * FROM Event")
for rows.Next() {
e:= new(Event)
err := rows.Scan(&e.Id, &e.CreatedAt, &e.ResolvedAt)
}
But when testing for Null there is a difference:
// A.
e.Resolved = (e.ResolvedAt != nil)
// B.
e.Resolved = (e.ResolvedAt.Valid)
In which case is SHOULD to use Null... type rather then pointer?