Nevermind, found a dirty solution besides have to restructure my entire
code-base to implement sql.NullString
I was having trouble when populating contacts (which is []contact) with the
individual query returns.
I went from...
contacts = append(contacts, contact)
To using...
nullString := ""
// - convert any fields with pointers to nil to pointers to a null string ¬
if contact.Email == nil {
contact.Email = &nullString
}
if contact.Phone == nil {
contact.Phone = &nullString
}
if contact.Ext == nil {
contact.Ext = &nullString
}
contacts = append(contacts, contact)
If anybody knows of a less-dirty way to handle this, or make that code
cleaner... please let me know. I don't like the way it's being handled
now, but at least it's working now. Thank you!
On Tuesday, August 22, 2017 at 5:48:09 PM UTC-5, Eric Brown wrote:
>
> Let's say I have a struct...
>
> type Contact struct {
> Id int `json:"id"`
> FirstName *string `json:"firstname"`
> LastName *string `json:"lastname"`
> Email *string `json:"email"`
> Phone *string `json:"phone"`
> Ext *string `json:"ext"`
> }
>
> I define contact...
> var contact Contact
>
> I put a query from the db such as and populate contact if no errors were
> returned...
> err := q.Scan(&contact.Id, &contact.FirstName, &contact.LastName, &contact
> .Email, &contact.Phone, &contact.Ext)
>
> Problem is... later I use this contact for something else in my code, and
> get a nil object exception for contact.Ext because it was nil in the db and
> doesn't actually contain anything... not even a "".
>
> How do I check this field for nil before processing it so I can handle it?
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.