标签语句

控制流 / 跳转语句

var firstMatchValue = -1
var array1 = []int{1, 2, 3}
var array2 = []int{2, 3, 4}

//A labeled break exits the outer loop, not just the inner one.
//Idiomatic in Go for nested loops; the alternative is a function
//that returns. Unlike goto, it cannot jump elsewhere.
FirstLoop:
for _, value1 := range array1 {
    for _, value2 := range array2 {
        if value1 == value2 {
            firstMatchValue = value2
            break FirstLoop
        }
    }
}
//firstMatchValue is 2
fmt.Println("firstMatchValue =", firstMatchValue)