горутины

This commit is contained in:
badkaktus 2019-10-07 21:33:54 +03:00
parent 0399e1400c
commit eed0984820
3 changed files with 19 additions and 19 deletions

View File

@ -19,7 +19,7 @@ Switch
Методы (Methods)
Интерфейсы (Interfaces)
Ошибки (Errors)
Goroutines
Горутины (Goroutines)
Channels
Channel Buffering
Channel Synchronization

View File

@ -1,4 +1,4 @@
// A _goroutine_ is a lightweight thread of execution.
// орутины_ - это легковесный тред.
package main
@ -15,25 +15,25 @@ func f(from string) {
func main() {
// Suppose we have a function call `f(s)`. Here's how
// we'd call that in the usual way, running it
// synchronously.
// Предположим, у нас есть вызов функции `f(s)`. Вот
// как мы бы вызвали её обычным способом, запустив
// синхронно.
f("direct")
// To invoke this function in a goroutine, use
// `go f(s)`. This new goroutine will execute
// concurrently with the calling one.
// Чтобы вызвать эту функцию в горутине, используйте
// `go f(s)`. Эта новая горутина будет выполняться
// одновременно с вызывающей фукнцией.
go f("goroutine")
// You can also start a goroutine for an anonymous
// function call.
// Вы так же можете вызывать анонимные функции в
// горутнах.
go func(msg string) {
fmt.Println(msg)
}("going")
// Our two function calls are running asynchronously in
// separate goroutines now. Wait for them to finish
// (for a more robust approach, use a [WaitGroup](waitgroups)).
// Наши две функции теперь выполняются асинхронно в
// отдельных горутинах. Дождитесь их окончания (для
// более надежного подхода используйте [WaitGroup](waitgroups)).
time.Sleep(time.Second)
fmt.Println("done")
}

View File

@ -1,7 +1,7 @@
# When we run this program, we see the output of the
# blocking call first, then the interleaved output of the
# two goroutines. This interleaving reflects the
# goroutines being run concurrently by the Go runtime.
# Когда мы запускаем эту программу, мы видим сначала
# вывод блокирующего вызова, а затем чередующийся вывод
# двух горутин. Это чередование отражает выполнение
# горутин, одновременно выполняемых средой выполнения Go.
$ go run goroutines.go
direct : 0
direct : 1
@ -12,5 +12,5 @@ goroutine : 1
goroutine : 2
done
# Next we'll look at a complement to goroutines in
# concurrent Go programs: channels.
# Далее мы рассмотрим дополнение к горутинам в
# параллельных программах Go: каналы.