1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-30 08:57:07 +02:00
revive/testdata/range-val-in-closure.go

50 lines
1.1 KiB
Go
Raw Normal View History

2018-09-27 17:21:10 +02:00
package fixtures
import "fmt"
func foo() {
mySlice := []string{"A", "B", "C"}
for index, value := range mySlice {
go func() {
fmt.Printf("Index: %d\n", index) // MATCH /loop variable index captured by func literal/
fmt.Printf("Value: %s\n", value) // MATCH /loop variable value captured by func literal/
}()
}
myDict := make(map[string]int)
myDict["A"] = 1
myDict["B"] = 2
myDict["C"] = 3
for key, value := range myDict {
defer func() {
fmt.Printf("Index: %d\n", key) // MATCH /loop variable key captured by func literal/
fmt.Printf("Value: %s\n", value) // MATCH /loop variable value captured by func literal/
}()
}
for i, newg := range groups {
go func(newg int) {
2022-02-15 23:46:24 +02:00
newg.run(m.opts.Context, i) // MATCH /loop variable i captured by func literal/
}(newg)
}
for i, newg := range groups {
newg := newg
go func() {
2022-02-15 23:46:24 +02:00
newg.run(m.opts.Context, i) // MATCH /loop variable i captured by func literal/
}()
}
}
func issue637() {
for key := range m {
myKey := key
go func() {
println(t{
key: myKey,
otherField: (10 + key), // MATCH /loop variable key captured by func literal/
})
}()
}
2018-09-27 17:21:10 +02:00
}