-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: Go 2: obtain struct field name in type-safe way #36332
Comments
Can you give an example of where a program would use this? In https://blog.golang.org/go2-here-we-come @griesemer lists three conditions for a language change:
I'm not sure this is an important issue for many people. |
1. Update multiple attributes with
|
I gather from your examples that this proposal would be type-safe compared to what we do now because if the field names were mis-typed or were changed, then the compiler would be able to flag that immediately rather than leave it to a function to validate the names using reflection at runtime. If so, it's a similar idea to C#'s I think this is an interesting (and potentially valuable) idea but, if it were to be entertained for Go, I'd prefer to see something more akin to a |
I feel like your examples leave out the most interesting part, which is the code that uses the field names. I guess that code must be written using the reflect package, and it must be looking up the fields by name or something like that. That code can already give an error if the name is incorrect, so I guess that this proposal is about moving an error from run time to compile time. Which I guess is what @alanfo said as well. Personally I think that |
While I agree the syntax proposed here is not very nice, a built in Edit the semantic could be much like in this proposal. |
This proposal permits compile-time type safety for struct field names, but that doesn't seem like a case that comes up very often. We can already do this; the only benefit here is compile-time type safety. This doesn't seem to "address an important issue for many people," as should be required for any language change. As an aside, if we had this feature, one would expect it work for all types, not just struct types. I guess that would lead us more toward the Also, this proposal does not have strong support based on emoji voting. For these reasons, this is a likely decline. Leaving open for four weeks for further comments. |
@ianlancetaylor Actually, struct field names do come up rather often in ORMs, where the struct's fields' names are often translated into names for the database columns. Now, we have to either use reflect, which is run time and has some performance issues, or maintain a manual or generated list of columns. For such cases, a built in |
@beoran Yes, I think |
No change in consensus, so closing. |
Good point
The text was updated successfully, but these errors were encountered: