Search code examples
postgresqlgosqlx

sqlx mapping to struct, how to solve "missing destination name"?


I am trying to map a table which looks like this:

Column Type Collation Nullable Default
time timestamp without time zone
NL double precision

to a struct like this:

type Price struct {
    time time.Time `db:"time"`
    NL float64 `db:"NL"`
}

with this code:

data := []Price{}
err = db.Select(&data, "" +
    "SELECT \"time\",\"NL\" FROM <TABLENAME> where \"time\" > '2020-01-01' order by \"time\" desc")
if err != nil {
    fmt.Println(err)
}

but I keep getting the error

missing destination name time in *[]main.Price

after some searching the fixes I applied in order of what I found (with above code as end result):

  • add the db:name to the struct
  • changed above include double quotes
  • checked case of column names that they matched

Those were all the common solutions here on SO and other places on the internet. I dont understand why this doesnt work, can anybody help me? Thanks!


Solution

  • Reflection tools in Go only expose struct fields with Capitalized (exported) names.

    Try changing time time.Time to Time time.Time.