-
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: spec: allow &5 to allocate and initialize int 5 #19966
Comments
From my personal experience, I use helper functions such as |
Note that all language changes are on hold until any Go2, so this will likely be low priority during future proposal reviews. |
If I recall correctly we had actually discussed this possibility back in the early days when we (@rsc in particular) designed the details of addressability. Note that even for composite literals, the use of |
Yeah I definitely wasn't expecting this to get taken up for go 1.9 or even at all. A common mistake this would prevent is: i := 5
myStruct := Settings{
A: &i,
B: &i,
}
// Then later someone else mutates myStruct.A and B also changes! I think this issue differs from the old email thread (which I hadn't found, thanks) in that there's an actual use case :) |
See also #9097. |
Related to #7054. The pattern for struct fields with *T for builtin types typically descends from the desire to distinguish between the zero-value and an unset value. |
Just an observation: if there were read-only types, then |
This is another a pattern I've seen to deal with this inline which isn't great: myStruct := Settings{
A: func(i int) *int { return &i }(5),
B: func(i int) *int { return &i }(5),
} @bradfitz: I'm lacking some understanding on what causes this to be a language change. Does the language change label indicate it wouldn't be backwards compatible, or that it more generally makes a change to the langauge spec? |
The LanguageChange label is applied to all proposed changes to the language, whether backward compatible or not. At this stage we are very unlikely to make any meaningful change to the language before Go 2. |
Use case: Many structs intended for json or other serialization use pointers to tell the difference between fields that have been set vs not set. It's currently awkward to declare instances of such structs. An example:
Today one generally does:
The proposal would be a language change to just allow:
Filing an issue to have a place to discuss (none of my searches turned up a similar feature request).
The text was updated successfully, but these errors were encountered: