You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2019/06/04 12:25:29 UTC
[GitHub] [pulsar-client-go] wolfstudy opened a new issue #13: [Bug Fix]
About go func(){} usage
wolfstudy opened a new issue #13: [Bug Fix] About go func(){} usage
URL: https://github.com/apache/pulsar-client-go/issues/13
In [impl_producer.go#L79-L84](https://github.com/apache/pulsar-client-go/blob/master/pulsar/impl_producer.go#L79-L84), i think the correct way of handling should be:
```
for partitionIdx, partition := range partitions {
go func(index int) {
prod, err := newPartitionProducer(client, partition, options, index)
c <- ProducerError{partitionIdx, prod, err}
}(partitionIdx)
}
```
test code:
```
package main
import (
"fmt"
"time"
)
func main() {
s := []string{"a", "b", "c"}
for _, v := range s {
go func() {
fmt.Println(v)
}()
}
time.Sleep(time.Second * 1000)
}
```
output:
```
c
c
c
```
As you can see, you can only get the value of the last loop before passing the copy value of the variable `v` into the anonymous function.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services