gobyexample/examples/range-over-channels/range-over-channels.go
2019-10-08 11:18:36 +03:00

26 lines
887 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// В [предыдущем](range) примере мы виделе как `for` и
// `range` позволяют перебирать базовые структуры. Мы
// так же можем использовать этот синтаксис для чтения
// значений из канала.
package main
import "fmt"
func main() {
// Мы будем итерировать 2 значения в канале `queue`.
queue := make(chan string, 2)
queue <- "one"
queue <- "two"
close(queue)
// Этот `range` будет перебирать каждый элемент
// полученный из канала `queue`. Но т.к. мы `закрыли`
// канал ранее, перебор элементов завершится после
// получения двух элементов.
for elem := range queue {
fmt.Println(elem)
}
}