каналы

This commit is contained in:
badkaktus 2019-10-07 21:58:34 +03:00
parent eed0984820
commit 8461d2c8e4
3 changed files with 20 additions and 20 deletions

View File

@ -20,7 +20,7 @@ Switch
Интерфейсы (Interfaces)
Ошибки (Errors)
Горутины (Goroutines)
Channels
Каналы (Channels)
Channel Buffering
Channel Synchronization
Channel Directions

View File

@ -1,7 +1,6 @@
// _Channels_ are the pipes that connect concurrent
// goroutines. You can send values into channels from one
// goroutine and receive those values into another
// goroutine.
// _Каналы_ это способ связи паралелльных горутин между
// собой. Вы можете послать сообщение в канал из одной
// горутины и получить его в другой.
package main
@ -9,18 +8,19 @@ import "fmt"
func main() {
// Create a new channel with `make(chan val-type)`.
// Channels are typed by the values they convey.
// Создаем новый канал - `make(chan val-type)`.
// Каналы типизируются в зависимости от значений,
// которые они передают.
messages := make(chan string)
// _Send_ a value into a channel using the `channel <-`
// syntax. Here we send `"ping"` to the `messages`
// channel we made above, from a new goroutine.
// _Отправьте_ значение в канал, используя `channel <-`.
// Здесь мы отправляем `"ping"` в канал `messages`,
// который мы сделали выше, из новой горутины.
go func() { messages <- "ping" }()
// The `<-channel` syntax _receives_ a value from the
// channel. Here we'll receive the `"ping"` message
// we sent above and print it out.
// Синтаксис `<-channel` _читает_ из канала. Здесь
// мы получим сообщение `"ping"`, которое мы
// отправили выше, и распечатаем его.
msg := <-messages
fmt.Println(msg)
}

View File

@ -1,10 +1,10 @@
# When we run the program the `"ping"` message is
# successfully passed from one goroutine to another via
# our channel.
# Когда мы запускаем программу, сообщение `"ping"` успешно
# передается из одной горутины в другую по нашему каналу.
$ go run channels.go
ping
# By default sends and receives block until both the
# sender and receiver are ready. This property allowed
# us to wait at the end of our program for the `"ping"`
# message without having to use any other synchronization.
# По-умолчанию, отправление и получение блокируются,
# пока оба отправителя и получателя готовы. Это
# свойство позволило нам ждать в конце нашей программы
# сообщения `"ping"` без использования какой-либо
# другой синхронизации.