github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/vet/test_rangeloop.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // This file contains tests for the rangeloop checker. 6 7 // +build vet_test 8 9 package main 10 11 func RangeLoopTests() { 12 var s []int 13 for i, v := range s { 14 go func() { 15 println(i) // ERROR "range variable i enclosed by function" 16 println(v) // ERROR "range variable v enclosed by function" 17 }() 18 } 19 for i, v := range s { 20 defer func() { 21 println(i) // ERROR "range variable i enclosed by function" 22 println(v) // ERROR "range variable v enclosed by function" 23 }() 24 } 25 for i := range s { 26 go func() { 27 println(i) // ERROR "range variable i enclosed by function" 28 }() 29 } 30 for _, v := range s { 31 go func() { 32 println(v) // ERROR "range variable v enclosed by function" 33 }() 34 } 35 for i, v := range s { 36 go func() { 37 println(i, v) 38 }() 39 println("unfortunately, we don't catch the error above because of this statement") 40 } 41 for i, v := range s { 42 go func(i, v int) { 43 println(i, v) 44 }(i, v) 45 } 46 for i, v := range s { 47 i, v := i, v 48 go func() { 49 println(i, v) 50 }() 51 } 52 // If the key of the range statement is not an identifier 53 // the code should not panic (it used to). 54 var x [2]int 55 var f int 56 for x[0], f = range s { 57 go func() { 58 _ = f // ERROR "range variable f enclosed by function" 59 }() 60 } 61 }