guregu / null

reasonable handling of nullable values
BSD 2-Clause "Simplified" License
1.86k stars 238 forks source link

Convert null.String to string #28

Closed magnus-eriksson closed 6 years ago

magnus-eriksson commented 6 years ago

There might be a super simple answer to this, but as a newbie at Go, I'm having an issue.

I'm fetching some data from MSSQL and have set up some struct properties as null.String since the DB can contain null values.

Before json encoding the structs, I need to manipulate them a bit and need to do some simple if statements, like: if row.x == "y" { //do stuff } but since row.x has the type null.String, that comparison fails with:

mismatched types null.String and string

If I try to convert it to string first: var x = string(row.x), I get:

cannot convert (type null.String) to type string

What is the best way to handle this?

magnus-eriksson commented 6 years ago

Never mind. Figured it out.
I can use var x = row.x.String to get the real string value.