码迷,mamicode.com
首页 > 其他好文 > 详细

range _ golang

时间:2015-03-13 16:02:55      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

range iterates over of elements in variety of data structures. Let‘s see how use range with some of the data structures we‘ve already leraned

package main

import (
    "fmt"
)

func main() {

    nums := []int{1, 2, 3}
    sum := 0

    for a, num := range nums {
        sum += num
        fmt.Println("a:", a)
    }

    fmt.Println("sum :", sum)

    for i, num := range nums {
        if num == 3 {
            fmt.Println("index:", i)
        }
    }

    kvs := map[string]string{"a": "apple", "b": "banana"}
    for k, v := range kvs {
        fmt.Printf("%s -> %s\n", k, v)
    }

    for i, c := range "go" {
        fmt.Println(i, c)
    }
}
a: 0
a: 1
a: 2
sum : 6
index: 2
b -> banana
a -> apple
0 103
1 111

总结

  1 : .....

range _ golang

标签:

原文地址:http://www.cnblogs.com/jackkiexu/p/4335173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!