-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Allow differentiating between a nil string and an empty string #44219
Comments
could you explain this a bit more? What problems? Could give some examples? |
There is no such thing as a nil string. Truly. You will find that
and
Both have a nil pointer and are therefore truly indistinguishable. You're asking for a fundamental change in the language. It's not going to happen. |
Of course Say I receive the following input from an http client
And I also have this input
And I have a struct with a Title and a Desc string field. I decode the input using the json lib into a struct.
How do I know if the user provided an empty description, or if the user did NOT provide a description? Another use case could be writing and reading using the SQL package, because you can have an empty string in a db, and an empty string in a DB (unless you mark its col as not null) |
Thank you so much for answering. Should I close the issue? |
I can see two possible meanings here. One is changing the zero value of the The other possible meaning is that we keep The usual way to distinguish a |
We have this problem with all types and distinguishing their zero values. ie integer 0, boolean false. Struct pointers are a good signal. Always check for nil before using! Pretty straight forward - if tedious. For marshalling, it's a bit tougher; we don't have a good json tag for "omit nil" (vs "omit empty"). And even then, sometimes you want the explicitly set nil pointer emitted (generating JSON for PATCH requests) to clear a field. I'm aware of no good options other than making wrappers for all struct types, that can indicate whether a field was set, or an unset nil, or explicit nil. |
With the now accepted generics we could introduce an Option[T] type into the standard library that would cover this use case. They would be similar in spirit to the Null* types in database/sql. |
Perhaps off-topic, but this is false. Type parameters do not give us sum types. |
@zephyrtronium I think @D1CED was suggesting
You don't need to choose presence when instantiating the type, but when making an instance. |
And the standard library database/sql solves this problem by having a host of NullXXX types that are not generic but have a bool field to record whether they are set or not. https://golang.org/pkg/database/sql/ |
This is a very interesting approach. We could eventually land at a generic (parametric type)
like @randall77 suggests. Interestingly, the language could add some sugar and use a special
and allow the retrieval of the data with sugar. If the data type is a primitive, then the compiler would just But that would be another proposal, and I dont know if it would fit well with the spirit of the lang |
Based on the discussion above, and the lack of support via emoji voting, this is a likely decline. Leaving open for four weeks for final comments. |
Performance has caused so many security issues. Personally I see nil as an unused marker in JSON, as an inferior API design. I use an additional bool, when needed. Or a marker. After all, even a name that apparently could be anything, will never actually be a long enough UUID. Crypto often relies on this property. This way, it is obvious and could even be easily detected when something is nil, when it was never meant to be. Personally I now use a function that checks err for nil and returns err.Error() or "". Avoiding panic potential, for the rare cases where you might want your api to return err as a string, whether nil or not. I would like to see less nils if anything. |
No change in consensus. |
Currently, there is no diferrence between declaring a string variable without initiallizing it and declaring an empty string
There is no way to know if the string was purpously set to an empty string, or was not actually
assigned for some reason. This could be problematic for JSON encoding and decoding.
Stripe encountered this issue and resolved it by requiring the users of the Stripe SDK to use a
custom string pointer type, and the library user ends up writing a lot boilerplate code.
Proposal
Given the Go string is actually implemented by an array at internally the string type uses a pointer, we could create a built in function (or method) to detect if said array (or pointer) is either nil, or some allocated space of size zero. It could look like this
This proposal requires work, but it could be potentially helpful to some users, but I welcome your feedback 😄
The text was updated successfully, but these errors were encountered: