在前面的例子中, 我们讲过 | |
![]() ![]() package main | |
import "fmt" | |
func main() { | |
我们将遍历在 | queue := make(chan string, 2) queue <- "one" queue <- "two" close(queue) |
| for elem := range queue { fmt.Println(elem) } } |
$ go run range-over-channels.go one two | |
这个例子也让我们看到,一个非空的通道也是可以关闭的, 并且,通道中剩下的值仍然可以被接收到。 |
下一个例子: Timer