Search code examples
gogorp

How to handle null values in gorp Select


I'm trying to get the users from a DB as follow,

var users []User
_, err := dbMap.Select(&users, "select id,username,acctstarttime,acctlastupdatedtime,acctstoptime from accounting order by id")

Here I'm using gorp. When there are null values present, this throws exception

 Select failed sql: Scan error on column index 3: unsupported driver -> Scan pair: <nil> -> *string 

How can I solve this issue?. Here I used gorp because of the ease of mapping the output to a struct array.


Solution

  • Make whatever acctstarttime maps to a pointer to the type instead of a value of the type.

    if the col is null, the pointer will be nil.

    that or you can use the sql.NullXXX types, but I usually don't like those since they make everything else weird.