-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
spec: extend Type assertions
section
#38180
Comments
I'm not sure quite what you are suggesting. The blog post looks right, but it's not saying anything that the language spec doesn't already say, albeit tersely. The language spec already says that the type must be identical. Do you think we should just add a couple more examples? |
Type assertions
section in the docType assertions
section
@ianlancetaylor thanks for your prompt reply! :)
I agree, the information is there but, as you said, it is probably packed in a way not easily understandable, especially for Go newbie like me (I'm also coming from a Java background and that doesn't help for sure). As additional context, before being bitten by the mentioned issue (i.e.
My suggestion would be to add at least one example of type assertion involving a pointer type and, if possible, put in words the subtle difference between pointer vs non-pointer type assertion. |
To be clear, I don't think there is any difference between pointer and non-pointer type assertion. An interface value holds a value of some type. That type can be a pointer type or a non-pointer type. Type assertions work exactly the same way. But I think it would be reasonable to add an example to the language spec of a type assertion to a pointer type. Leaving for @griesemer to decide. |
What @ianlancetaylor said. I think the blog is at least confusing when it says "keep in mind that a pointer to a type is not the same as the type itself". It seems to imply that this is unusual behavior, which it is not. I suspect the confusion comes from not being intimately familiar with the notion of a pointer type, which is understandable when coming from "pointer-less" types such as Java or JavaScript (never mind that these languages use pointers everywhere under the covers but try to hide that, which leads to other confusions). I've reread the spec section on type assertions and it seems pretty clear. It talks in general terms about a type Leaving open in case somebody wants to suggest a good pointer example. Send any such CL to me. Thanks. |
Today I stubled upon this issue and I was hoping to find, in the reference doc, more examples of type assertions on pointer types but, unfortunately, there seems to be none.
Would it make sense to extend the current Type assertions section with this?
(BTW I'm a Go newbie so apologies in advance if I missed something in the existing doc!)
The text was updated successfully, but these errors were encountered: