-
Notifications
You must be signed in to change notification settings - Fork 183
Consider pointers to string (Int, etc) rather than NullString (Int, etc) #32
Comments
The coupling is unfortunate, but unavoidable now. |
Not sure if this is what you're referring to, but struct properties can be used in scan, e.g., |
Yes, but you can't use the struct directly. You don't pass the struct, you have to pass references to each field. |
I'm not sure I follow this. Consider: type foo struct {
bar *string
baz *int
}
f := foo{}
// ...
err = rows.Scan(f.bar, f.baz) If the first column is null, isn't it going to throw an error "can't scan null into a string" as usual? Are you suggesting a way to work around this somehow? |
@xaprb don't pass pointers that way - they are And - as I said - it's tedious. See below. type foo struct {
bar *string
baz int
}
f := foo{}
ns := sql.NullString{}
// ...
err = rows.Scan(&ns, &f.baz)
// ... error handling
if ns.Valid {
*f.bar = ns.String
} |
True. Although strictly speaking, if I'll close this now... it's one for the history books, right? Reopen if I'm wrong. |
You are right - and let's keep it closed. |
Using sql.NullString as a property type leaves a bad taste in the mouth when designing structs, as it seems to tightly couple SQL to models that may generally be independent of SQL.
I've found that using
*string
(*int
, etc) is more palatable. With automatic dereferencing, it hasn't been much of an issue. I'm curious re: your take on this, and re: your thoughts on including it in the section on NULL values.The text was updated successfully, but these errors were encountered: