range over channel _ golang

In a previous example we saw how for and range provide iteration over basic data structures. We can alse use this syntax to iterate over values received from a channel

package main

import (
    "fmt"
)

func main() {

    queue := make(chan string, 2)
    queue <- "one"
    queue <- "two"
    close(queue)

    for elem := range queue {
        fmt.Println(elem)
    }

}
one
two

总结 :

  1 : ......

posted on 2015-03-17 13:50  xjk112  阅读(211)  评论(0编辑  收藏  举报