Answer the question
In order to leave comments, you need to log in
How do you solve the sql.NullString problem || sql.NullInt64?
It often happens in relational subds that when selecting records from the database, a field with a null value is returned.
Go does not treat strings and numbers as nil, so there is an auxiliary type sql.Null{type}
But if you return data in json format, then the use of this type becomes unnecessary because, in javascript, there is null.
Now I'm converting sql.NullString to manual. There is also an option in sql itself to return not null, but an empty string (if it is a string),
but for some reason I think it will be difficult to debug in the future.
How do you solve this problem?
Answer the question
In order to leave comments, you need to log in
If you only need to send data from the database to the outside in the form of json, then you can use the same structure as the sql model and the json format. Then make nullable fields pointers to the corresponding types (*string, *int64), in the standard one they are processed correctly and where you have NULL in sql - in the internal structure it will be nil and in the resulting json - null.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question