I recently noticed that I had done this:
for t, ts := range timespans {
// remove current item
if t+1 < len(timespans) {
timespans = append(timespans[:t], timespans[t+1:]...)
} else {
timespans = timespans[:t]
}
where
var timespans []TimeSpan
and
type TimeSpan [2]time.Time
How does range
's work internally?
Does it work like a for i:=0; i<42; i++
loop (and skip items) or does it range over a copy of timespans
, as it looked when the loop first started, or something else?
Just found the answer in the language specification.
The range expression is evaluated once before beginning the loop[...]`
So it operates on a copy. Awesome!
It works on a copy of the slice, you can modify the slice's data in place but it will ignore append and such.