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
net: IPNet.String() returns an unexpected string value '<nil>' #39516
Comments
Thank you for the report and for the clear code example! I don't think an empty string would be any less arbitrary. I might even argue it could be ok for String() to panic for an uninitialized IPNet (nil fields). Also, the string "<nil>" is not that uncommon for things similar to this. fmt for example will output "<nil>" for nil pointers and for String() pointer receivers that panic because of dereferencing a nil pointer (Example: https://play.golang.org/p/f2gNtD7W8Pl). So, I would say returning "<nil>" is an option as good as any, and the function is good as it is. How did you end up with an unitialized IPNet in the first place? The example code you provided looks like a mistake as it assigns to the IP the bytes of the string representation of 127.0.0.1 ([]byte("127.0.0.1"), when it should be []byte{127,0,0,1} or even clearer, IPv4(127, 0, 0, 1). Perhaps the net.ParseCIDR() function is what you are looking for? Using that, you would not end up with a partially initialized IPNet. |
I agree with @antong's helpful response here. I'm closing this, as I don't believe this is an issue with the standard library. Please comment if I am mistaken. |
I don't know if formally this falls under Go 1 compatibility promise. For IPNet.String, this
WDYT? |
Another consistent position to take could be that
Another benefit here is that the word "nil" will no longer be confusing for a nil Again, not sure this is OK with compatibility promise (de jure not sure because not documented, de facto likely to break something for somebody)... (None of this is enough to make |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Not applicable.
What did you do?
When calling the .String() method on net.IPNet the method will return a string containing the text "<nil>" rather than a zero-value string or a nil pointer. This arbitrary string behavior is not documented and was an unexpected response to indicate an inability to string the IPNet data.
Can this be changed to return a proper string zero-value?
What did you expect to see?
An empty string.
What did you see instead?
An arbitrary string containing the text "<nil>".
The text was updated successfully, but these errors were encountered: