Closed
Description
What version of Go are you using (go version
)?
--> go version go1.13 darwin/amd64
Does this issue reproduce with the latest release?
--> Yes
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/gonyi/Library/Caches/go-build" GOENV="/Users/gonyi/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GONOPROXY="github.com/gonyyi" GONOSUMDB="github.com/gonyyi" GOOS="darwin" GOPATH="/Users/gonyi/go" GOPRIVATE="github.com/gonyyi" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jn/9zs9p73j7_gf3mzpdx4frmmd_05z01/T/go-build626678608=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
work := []string{"1mail", "2commute", "3coding", "4debugging", "5coffee"}
for _, v := range work {
go func(){
fmt.Println(v)
}()
}
I was doing simple iteration of work thru channel with go routine.
I was expect WORK function will receive those 5 items, however, it was repeating likely last item in the work slice as below.
5coffee
5coffee
5coffee
5coffee
5coffee
But, instead of having anonymous in for-loop, if I call outside function, it gives expected result:
work := []string{"1homework", "2commute", "3coding", "4debugging", "5coffee"}
for _, v := range work {
go fmt.Println(v)
//go func(){
// fmt.Println(v)
//}()
}
I think this is by the design, but I wonder if it makes more sense to have identical result..
What did you expect to see?
1homework
4debugging
5coffee
2commute
3coding
What did you see instead?
5coffee
5coffee
5coffee
5coffee
5coffee