каналы

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) Интерфейсы (Interfaces)
Ошибки (Errors) Ошибки (Errors)
Горутины (Goroutines) Горутины (Goroutines)
Channels Каналы (Channels)
Channel Buffering Channel Buffering
Channel Synchronization Channel Synchronization
Channel Directions 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 package main
@ -9,18 +8,19 @@ import "fmt"
func main() { func main() {
// Create a new channel with `make(chan val-type)`. // Создаем новый канал - `make(chan val-type)`.
// Channels are typed by the values they convey. // Каналы типизируются в зависимости от значений,
// которые они передают.
messages := make(chan string) messages := make(chan string)
// _Send_ a value into a channel using the `channel <-` // _Отправьте_ значение в канал, используя `channel <-`.
// syntax. Here we send `"ping"` to the `messages` // Здесь мы отправляем `"ping"` в канал `messages`,
// channel we made above, from a new goroutine. // который мы сделали выше, из новой горутины.
go func() { messages <- "ping" }() go func() { messages <- "ping" }()
// The `<-channel` syntax _receives_ a value from the // Синтаксис `<-channel` _читает_ из канала. Здесь
// channel. Here we'll receive the `"ping"` message // мы получим сообщение `"ping"`, которое мы
// we sent above and print it out. // отправили выше, и распечатаем его.
msg := <-messages msg := <-messages
fmt.Println(msg) fmt.Println(msg)
} }

View File

@ -1,10 +1,10 @@
# When we run the program the `"ping"` message is # Когда мы запускаем программу, сообщение `"ping"` успешно
# successfully passed from one goroutine to another via # передается из одной горутины в другую по нашему каналу.
# our channel.
$ go run channels.go $ go run channels.go
ping 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"` без использования какой-либо
# другой синхронизации.