the problem is that loops like this one don’t do what they look like they do:
var all []*Item
That is, this code has a bug. After this loop executes, all contains len(items) identical pointers, each pointing at the same Item, holding the last value iterated over. This happens because the item variable is per-loop, not per-iteration: &item is the same on every iteration, and item is overwritten on each iteration.
https://github.com/golang/go/discussions/56010--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5d88208e-fbbf-44d5-b693-50deff176fedn%40googlegroups.com.
Thanks for a very succinct response.
So if I understand the CL, there will be no change in behaviour in 1.21, unless you set GOEXPERIMENT=loopvar
- Amnon
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/aba5e0bd-c676-45f0-a7b7-ce6e23985124n%40googlegroups.com.
The "scary" thing is, that if people don't have enough tests, they are probably not aware of such a bug, or can they still be aware somehow?
Little late but I have to say the rational seems foolish :
- Currently 25% of gophers who didn't read the language spec experienced a 'bug' and got unexpected behavior..
- Solution is to change the language spec and behavior (despite breaking go back compat promise)
- ...
- Cut to the future : 25% of gophers who didn;t read the language spec experienced a 'bug' and got unexpected behaviour..
Expect to have to explain why all and all2 are different in this example :for _, i := range []int{1, 2, 3, 4} {
all = append(all, &i)
}
var j int
for _, j = range []int{1, 2, 3, 4} {
all2 = append(all2, &j)
}
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d5cc7e82-2928-48b1-b4ca-8b7089b2c8aen%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANrC0BiizSBnq5EiadZ5t026-YSWAK8AuFVt%3DCVYp9f2oE9Mng%40mail.gmail.com.
Resending to the mailing list as that was my intention but I errored
again. Did the gmail UI changed again?
---------- Forwarded message ---------
From: Jan Mercl <0xj...@gmail.com>
Date: Tue, Apr 11, 2023 at 9:11 AM
Subject: Re: [go-nuts] Redfining loop variable semantics - what's the plan?
To: Axel Wagner <axel.wa...@googlemail.com>
On Tue, Apr 11, 2023 at 7:46 AM 'Axel Wagner' via golang-nuts
<golan...@googlegroups.com> wrote:
> You shouldn't *have* to read the language spec to understand what Go code does.
This is the most strange claim I have read about Go ever.
You *must* read the language spec to understand what any programming
language does. You *cannot* just guess because "other languages" do
somethíng or use similar approaches and then be surprised you have
guessed wrong. And as can be seen often, blame the language designers
they didn't meet your unfounded expectations.
What bothers me personally about the proposal and why I think it's a
bad idea: The semantics will be defined by metadata. Any Go code
snippet with such a loop, existing or future, all over the
Internet/books/blogs, ... will become ambiguous in what it does
without those metadata, which are not usually/always available
alongside. You can say "other languages have that problem as well, see
the different C, Perl, Python, you name it... versions". And you would
be right. The point is that the Go 1 compatibility promise gave us the
nice property of Go not suffering from such problems.
I oppose the idea of introducing such a problem into the language
voluntarily, 10+ years later.
Maybe it would be better to bite the bullet, introduce Go 2 and
embrace the shizma. /s
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-UQaK1dBvo-690Z1VZDZ%2B%2BKL7gQGWi5Mq3G4x4mrxZuyg%40mail.gmail.com.