github.com/songshiyun/revive@v1.1.5-0.20220323112655-f8433a19b3c5/testdata/range-val-in-closure.go (about) 1 package fixtures 2 3 import "fmt" 4 5 func foo() { 6 mySlice := []string{"A", "B", "C"} 7 for index, value := range mySlice { 8 go func() { 9 fmt.Printf("Index: %d\n", index) // MATCH /loop variable index captured by func literal/ 10 fmt.Printf("Value: %s\n", value) // MATCH /loop variable value captured by func literal/ 11 }() 12 } 13 14 myDict := make(map[string]int) 15 myDict["A"] = 1 16 myDict["B"] = 2 17 myDict["C"] = 3 18 for key, value := range myDict { 19 defer func() { 20 fmt.Printf("Index: %d\n", key) // MATCH /loop variable key captured by func literal/ 21 fmt.Printf("Value: %s\n", value) // MATCH /loop variable value captured by func literal/ 22 }() 23 } 24 25 for i, newg := range groups { 26 go func(newg int) { 27 newg.run(m.opts.Context, i) // MATCH /loop variable i captured by func literal/ 28 }(newg) 29 } 30 31 for i, newg := range groups { 32 newg := newg 33 go func() { 34 newg.run(m.opts.Context, i) // MATCH /loop variable i captured by func literal/ 35 }() 36 } 37 } 38 39 func issue637() { 40 for key := range m { 41 myKey := key 42 go func() { 43 println(t{ 44 key: myKey, 45 otherField: (10 + key), // MATCH /loop variable key captured by func literal/ 46 }) 47 }() 48 } 49 }