Hi all,

I am a little confused about the parameters of Go closure. For example:  

package main
> import (
> "fmt"
> "time"
> )
> func main() {
> for i := 1; i <= 2; i++ {
> go func(j int) {
> fmt.Println(j, i)
> }(i)
> }
> time.Sleep(time.Second)
> }

The running result is:

> 1 3
> 2 3 

 
Except "go func(j int) { fmt.Println(j, i)}(i)", I find the following code 
isalso valid:

> go func(i int) { fmt.Println(i)}(i)
> go func(int) { fmt.Println(i)}(i)

I refer to spec, but only find https://golang.org/ref/spec#Function_literals, 
and it doesn't seem to elaborate the parameter rules of closure very 
clearly.

Could any help to explain it in detail? Thanks very much in advance!

Best Regards
Nan Xiao

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to