Hello, first of all, please accept many thanks for the great book you wrote! It's awesome to have all those gotchas collected in one place and so well explained :)
I would like to ask for a further explanation of the statement in the book that made me a bit confused though. It says that the range operator copies the slice/array/channel being iterated over (ie the range clause) on its first (and only) evaluation just before the iteration. I cannot find that in the relevant part of the Golang spec. My understanding was, on the other hand, the for-range behaves that way because it's the v (the local iterator variable) that is a copy of the slice element (not that the whole iterable thing gets copied). Please see the following snippet: https://go.dev/play/p/tEE8fW6Jxrk.
Hello, first of all, please accept many thanks for the great book you wrote! It's awesome to have all those gotchas collected in one place and so well explained :)
I would like to ask for a further explanation of the statement in the book that made me a bit confused though. It says that the
range
operator copies the slice/array/channel being iterated over (ie therange clause
) on its first (and only) evaluation just before the iteration. I cannot find that in the relevant part of the Golang spec. My understanding was, on the other hand, the for-range behaves that way because it's thev
(the local iterator variable) that is a copy of the slice element (not that the whole iterable thing gets copied). Please see the following snippet: https://go.dev/play/p/tEE8fW6Jxrk.Referring to the code samples in this repo here, such a statement can be found in the book as the comment eg. to this one (or a couple of other snippets in the 4.2.x chapter): https://github.com/teivah/100-go-mistakes/blob/b2152d01bbe3397f7df3bc8af10a62a6f8e07aa9/04-control-structures/31-range-loop-arg-evaluation/arrays/main.go#L5-L13