georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.24k stars 67 forks source link

.Get() #63

Closed robfordww closed 2 years ago

robfordww commented 2 years ago

I have noticed that the.Get()method of Scany and Sqlx varies when you try to scan a select now() into a var t time.Time. Scany.Get() treats the &t as a struct with missing fields, whilesqlx.Get() does write the value into the variable. Is this "by design"? I noticed this when switching from Sqlx to Scany.

georgysavva commented 2 years ago

Hello! This is indeed a limitation in the current scany implementation. See this comment for more details: https://github.com/georgysavva/scany/issues/33#issuecomment-813221294 I have plans to fix it in the future, but for now, a workaround to scan using scany would be:

type Row struct {
    T time.Time
}
row := &Row{}
if err := pgxscan.Get(ctx, pgxConn, row, "select now() as t"); err != nil {
    return nil, err
}
fmt.Println(row.T)