以下哪项是Go中用于控制循环的有效关键字?

时间:2016-05-27 12:51:14

标签: go flow-control

我看到了这个问题,正确的答案是' for和范围'。

for 语句是Go中唯一可用的循环语句,而range关键字允许您迭代列表中的项目,如数组或映射。为了理解它,您可以将范围关键字转换为,用于的每个索引。

//for loop
package main

import "fmt"

func main() {
for i := 0; i < 5; i++ {
    fmt.Println("Value of i is now:", i)
    }
}

 //range is used inside a for loop


a := [...]string{"a", "b", "c", "d"}
for i := range a {
    fmt.Println("Array item", i, "is", a[i])
}
capitals := map[string] string {"France":"Paris", "Italy":"Rome",     "Japan":"Tokyo" }
for key := range capitals {
    fmt.Println("Map item: Capital of", key, "is", capitals[key])
    }

//range can also return two items, the index/key and the corresponding value 
for key2, val := range capitals {
    fmt.Println("Map item: Capital of", key2, "is", val)
    }

1 个答案:

答案 0 :(得分:0)

我认为问题是关于For Loop的不同形式:
简单循环变体工作样本:

<html>

<form>
    <p><input type="checkbox" name="option[]" class="selectAll"> Select All</p>
    <p><input type="checkbox" name="option[]" class="option"> Option 1</p>
    <p><input type="checkbox" name="option[]" class="option"> Option 2</p>
    <p><input type="checkbox" name="option[]" class="option"> Option 3</p>

    <button type="submit">Submit</button>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<script>
    // event handler for when an item with the 'selectAll' is changed
    $(".selectAll").change(function(){
        // set the 'checked' property of items with the 'option' class
        // to have the same value as the 'selectAll' checkbox
        $(".option").prop('checked', $(this).prop("checked"));
    });
</script>

</html>

用于数组,切片,字符串,地图,使用范围关键字的通道:

package main

import "fmt"

func main() {
    //0 1 2 3 4 5 6 7 8 9
    for i := 0; i < 10; i++ {
        fmt.Print(i, " ")
    }
    fmt.Println()

    i := 0
    for ; i < 10; i++ {
        fmt.Print(i, " ")
    }
    fmt.Println()

    for i := 0; i < 10; {
        fmt.Print(i, " ")
        i++
    }
    fmt.Println()

    //2 4 6 8 10 12 14 16 18 20
    for i := 1; ; i++ {
        if i&1 == 1 {
            continue
        }
        if i == 22 {
            break
        }
        fmt.Print(i, " ")
    }
    fmt.Println()

    i = 0
    for {
        fmt.Print(i, " ")
        i++
        if i == 10 {
            break
        }

    }
    fmt.Println()

    for i, j := 0, 0; i < 5 && j < 10; i, j = i+1, j+2 {
        fmt.Println(i, j)
    }
}

和Label for break标签并继续标签:

package main

import "fmt"

func main() {
    ary := [5]int{0, 1, 2, 3, 4}
    for index, value := range ary {
        fmt.Print("ary[", index, "] =", value, " ")
    }
    fmt.Println()
    for index := range ary {
        fmt.Print("ary[", index, "] =", ary[index], " ")
    }
    fmt.Println()
    for _, value := range ary {
        fmt.Print(value, " ")
    }
    fmt.Println()

    slice := []int{20, 21, 22, 23, 24, 25, 26, 27, 28, 29}
    for index, value := range slice {
        fmt.Println("slice[", index, "] =", value)
    }
    fmt.Println()

    str := "Hello"
    for index, value := range str {
        fmt.Println("str[", index, "] =", value)
    }
    fmt.Println()

    mp := map[string]int{"One": 1, "Two": 2, "Three": 3}
    for key, value := range mp {
        fmt.Println("map[", key, "] =", value)
    }
    fmt.Println()

    ch := make(chan int, 10)
    for i := 0; i < 10; i++ {
        ch <- i
    }
    close(ch)

    for i := range ch {
        fmt.Print(i, " ")
    }
    fmt.Println()
}