How to check sql.NullString?

I am reading data from the database where I have a field type of sql.NullString. How can I check if the sql.NullString is empty or not?

1 Like

https://golang.org/pkg/database/sql/#NullString

1 Like

You could write a func and pass in the field as an argument. This will allow you to do other things too, like populate a struct…

Sudo code (untested, written in this post but should work.)

func (s sql.NullString) isNull string {
if !s.Valid {
    return ""
   }
    return s.Value
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.