Proper way for handling null value (nil) #48
-
Hi, How could we handle null value ? Sometimes we need to make the difference between empty string and null value for example (usually, the key is not set in the mongo document as far as I can see). A more relevant example is maybe the Int default value where 0 can be a valid value for our business logic, but "null" can as well. Does a package like this could help ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @SebUndefined // User contains user information
type User struct {
mgm.DefaultModel `bson:",inline"`
Name *string `json:"name"`
Age *int `bson:"age"`
} And if you want to omit empty values in your DB (by default nil values will be as // User contains user information
type User struct {
mgm.DefaultModel `bson:",inline"`
Name *string `json:"name,omitempty"`
Age *int `bson:"age,omitempty"`
} |
Beta Was this translation helpful? Give feedback.
Hi @SebUndefined
Just use pointers:
And if you want to omit empty values in your DB (by default nil values will be as
null
value on DB), you can useomitempty
tag on your field.e.g.,