-
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
proposal: spec: add structfield type and syntax to directly access a structs structfield #70564
Comments
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
You can, of course, use the reflect package for this kind of thing. |
Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes |
Python does this with a key func, https://docs.python.org/3/howto/sorting.html#key-functions func UserBy[V comparable](users []User, v V, key func(User) V) User {
index := slices.IndexFunc(users, func(u User) bool {
return key(u) == v
})
if index == -1 {
return User{}
}
return users[index]
} |
This introduces new syntax See also #59101, a previously declined proposal. That is at least syntactically closer to something that we might adopt. Therefore, this is a likely decline. Leaving open for four weeks for further comments. |
No further comments, so declined. |
After revisiting this i prefer moving the .. after the User. func main() {
} |
Proposal Details
Problem:
I want to write generic functions without manually rewriting functions for each struct field i am comparing.
structfield
typeany
would work in the definition.Current state of Go:
Proposed go code:
..User.FirstName
refers to the FirstName field of the User struct....
or..
in certain cases.The text was updated successfully, but these errors were encountered: