From 3a7af42519e75f6d2d6c87a9ffdba897171b3e58 Mon Sep 17 00:00:00 2001 From: badkaktus Date: Mon, 7 Oct 2019 22:45:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BB=D0=BE=D0=BA=D0=B8?= =?UTF-8?q?=D1=80=D1=83=D1=8E=D1=89=D0=B8=D0=B5=20=D0=BA=D0=B0=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples.txt | 2 +- .../non-blocking-channel-operations.go | 35 ++++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/examples.txt b/examples.txt index b9f1167..b78b0d8 100644 --- a/examples.txt +++ b/examples.txt @@ -26,7 +26,7 @@ Switch Направления канала (Channel Directions) Select Тайм-ауты (Timeouts) -Non-Blocking Channel Operations +Неблокируемые операции в каналах (Non-Blocking Channel Operations) Closing Channels Range over Channels Timers diff --git a/examples/non-blocking-channel-operations/non-blocking-channel-operations.go b/examples/non-blocking-channel-operations/non-blocking-channel-operations.go index 2429da1..a2a1693 100644 --- a/examples/non-blocking-channel-operations/non-blocking-channel-operations.go +++ b/examples/non-blocking-channel-operations/non-blocking-channel-operations.go @@ -1,7 +1,8 @@ -// Basic sends and receives on channels are blocking. -// However, we can use `select` with a `default` clause to -// implement _non-blocking_ sends, receives, and even -// non-blocking multi-way `select`s. +// Стандартные отправители и получатели в каналах +// являются блокирующими.Тем не менее, мы можем +// использовать `select` с выбором `по умолчанию` для +// реализации _неблокирующих_ отправителей, получателей +// и даже неблокирующего множественного `select`'а. package main @@ -11,10 +12,10 @@ func main() { messages := make(chan string) signals := make(chan bool) - // Here's a non-blocking receive. If a value is - // available on `messages` then `select` will take - // the `<-messages` `case` with that value. If not - // it will immediately take the `default` case. + // Вот неблокирующее получение. Если значение + // доступно в `messages`, тогда `select` выберет + // `<-messages` с этим значением. Если нет, он + // сразу же примет случай `по-умолчанию`. select { case msg := <-messages: fmt.Println("received message", msg) @@ -22,10 +23,11 @@ func main() { fmt.Println("no message received") } - // A non-blocking send works similarly. Here `msg` - // cannot be sent to the `messages` channel, because - // the channel has no buffer and there is no receiver. - // Therefore the `default` case is selected. + // Неблокирующая отправка работает аналогично. + // Здесь `msg` не может быть отправлено в канал + // `messages`, потому что у канала нет буфера и + // нет получателя. Поэтому выбирается действие + // `по-умолчанию`. msg := "hi" select { case messages <- msg: @@ -34,10 +36,11 @@ func main() { fmt.Println("no message sent") } - // We can use multiple `case`s above the `default` - // clause to implement a multi-way non-blocking - // select. Here we attempt non-blocking receives - // on both `messages` and `signals`. + // Мы можем использовать несколько `case`'ов перед + // `действием по-умолчанию` для реализации + // многоцелевого неблокирующего выбора. Здесь мы + // пытаемся получить неблокирующее получение + // `messages` и `signals`. select { case msg := <-messages: fmt.Println("received message", msg)