Re: [go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Silvan Jegen
Jan Mercl <0xj...@gmail.com> wrote: > On Sun, Jan 12, 2020 at 10:10 AM Silvan Jegen wrote: > > > So the declaration of the variables in the for loop itself is in outer > > scope compared to the body of the for loop? > > The outer scope begins immediately after the keyword "for". The inner > one

Re: [go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Jan Mercl
On Sun, Jan 12, 2020 at 10:10 AM Silvan Jegen wrote: > So the declaration of the variables in the for loop itself is in outer > scope compared to the body of the for loop? The outer scope begins immediately after the keyword "for". The inner one is an ordinary block scope and it begins

Re: [go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Dan Kortschak
On Sun, 2020-01-12 at 10:10 +0100, Silvan Jegen wrote: > So the declaration of the variables in the for loop itself is in > outer scope compared to the body of the for loop? Yes. > In that case, redeclaring them in the inner scope (== the loop body) > would not be allowed either, no? What? The

Re: [go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Silvan Jegen
Dan Kortschak wrote: > The gopher operator is allowed to be used because the body of the for > loop is a new scope. In the second example, the a, b := b, a is not in > a new scope. So the declaration of the variables in the for loop itself is in outer scope compared to the body of the for loop?

Re: [go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Dan Kortschak
The gopher operator is allowed to be used because the body of the for loop is a new scope. In the second example, the a, b := b, a is not in a new scope. On Sun, 2020-01-12 at 09:09 +0100, Silvan Jegen wrote: > Hi fellow gophers > > The following code compiles > > > package main > > import (

Re: [go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Ian Davis
The range clause introduces a new scope. See this version of your second example: https://play.golang.org/p/UXI_w6B4DW5 -- Ian On Sun, 12 Jan 2020, at 8:09 AM, Silvan Jegen wrote: > Hi fellow gophers > > The following code compiles > > > package main > > import ( > "fmt" > ) > > func

[go-nuts] Language spec question: why is k, v := k, v allowed in a for loop with range?

2020-01-12 Thread Silvan Jegen
Hi fellow gophers The following code compiles package main import ( "fmt" ) func main() { mymap := map[string]int{"a": 1, "b": 2} for k, v := range mymap { k, v := k, v fmt.Printf("k %s, v: %d\n", k, v) } } while this one