итерация в каналах

This commit is contained in:
badkaktus 2019-10-08 11:18:36 +03:00
parent c16c142918
commit 33465a0288
2 changed files with 11 additions and 12 deletions

View File

@ -1,7 +1,7 @@
// In a [previous](range) example we saw how `for` and
// `range` provide iteration over basic data structures.
// We can also use this syntax to iterate over
// values received from a channel.
// В [предыдущем](range) примере мы виделе как `for` и
// `range` позволяют перебирать базовые структуры. Мы
// так же можем использовать этот синтаксис для чтения
// значений из канала.
package main
@ -9,16 +9,16 @@ import "fmt"
func main() {
// We'll iterate over 2 values in the `queue` channel.
// Мы будем итерировать 2 значения в канале `queue`.
queue := make(chan string, 2)
queue <- "one"
queue <- "two"
close(queue)
// This `range` iterates over each element as it's
// received from `queue`. Because we `close`d the
// channel above, the iteration terminates after
// receiving the 2 elements.
// Этот `range` будет перебирать каждый элемент
// полученный из канала `queue`. Но т.к. мы `закрыли`
// канал ранее, перебор элементов завершится после
// получения двух элементов.
for elem := range queue {
fmt.Println(elem)
}

View File

@ -2,6 +2,5 @@ $ go run range-over-channels.go
one
two
# This example also showed that it's possible to close
# a non-empty channel but still have the remaining
# values be received.
# Этот пример так же демонстрирует, что возможно
# прочитать данные из канала уже после его закрытия.