NULL data type in MongoDB

What’s the Go data type i need to use to insert a value in mongoDB of type Null ?.

I have following struct
type Case struct {
Sender string bson:"Sender"
Reason string bson:"Reason" . —> I need to insert this value as null and type as Null in MongoDB

What data type i should declare in Golang to achieve this?. You help is appreciated.


My guess is *string.

@nathankerr Thanks. It worked

1 Like

Far better is avoid null. Empty string is better than pointer.

Empty string does not let you differentiate between when the string was not set and when it is actively set to "". Null is one method to determine the difference.

Another example that also explains why you might need to make this difference is:

Yes, I know. But often I do not need this information: difference between empty and nil. And often is this information important (for example information about sex: male, female, NIL) and is better add a new special value for NIL value (male, female, unknown).

And in Go I must create pointers when I want manage nil values and it can be place for create a lot of bugs. This reason, why I want avoid pointers (and nil values) if it is possible.

Pointers are not the only way to manage nil/null values in Go. The article I linked to tracks the validity of the value as part of the type. The sql.Null* types use a similar method.

I also try to avoid nulls and pointers when possible because they are fundamentally more complicated than non-nullable types. I also don’t shy away from them when they are needed.

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