This commit is contained in:
badkaktus 2019-10-07 22:30:13 +03:00
parent a791406292
commit 7cd2fb92eb
2 changed files with 16 additions and 14 deletions

View File

@ -1,6 +1,6 @@
// Go's _select_ lets you wait on multiple channel
// operations. Combining goroutines and channels with
// select is a powerful feature of Go.
// Go's _select позволяет вам ждать нескольких
// операций на канале. Сочетание горутин и каналов
// с помощью select'а - мощная функция Go.
package main
@ -11,13 +11,14 @@ import (
func main() {
// For our example we'll select across two channels.
// В нашем примере мы будем выбирать между двумя
// каналами.
c1 := make(chan string)
c2 := make(chan string)
// Each channel will receive a value after some amount
// of time, to simulate e.g. blocking RPC operations
// executing in concurrent goroutines.
// Каждый канал получит значение через некоторое время,
// например, для моделирования блокировки RPC-операций,
// выполняемых в параллельных горутинах.
go func() {
time.Sleep(1 * time.Second)
c1 <- "one"
@ -27,8 +28,9 @@ func main() {
c2 <- "two"
}()
// We'll use `select` to await both of these values
// simultaneously, printing each one as it arrives.
// Мы будем использовать `select`, чтобы ожидать
// оба значения одновременно, печатая каждое из
// них по мере поступления.
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:

View File

@ -1,10 +1,10 @@
# We receive the values `"one"` and then `"two"` as
# expected.
# Мы получаем значние `"one"` и потом `"two"`, как
# и ожидалось.
$ time go run select.go
received one
received two
# Note that the total execution time is only ~2 seconds
# since both the 1 and 2 second `Sleeps` execute
# concurrently.
# Обратите внимание, что общее время выполнения
# составляет всего ~2 секунды, так как и 1, и 2 секунды
# `Sleeps` выполняются одновременно.
real 0m2.245s