горутины

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) Методы (Methods)
Интерфейсы (Interfaces) Интерфейсы (Interfaces)
Ошибки (Errors) Ошибки (Errors)
Goroutines Горутины (Goroutines)
Channels Channels
Channel Buffering Channel Buffering
Channel Synchronization Channel Synchronization

View File

@ -1,4 +1,4 @@
// A _goroutine_ is a lightweight thread of execution. // орутины_ - это легковесный тред.
package main package main
@ -15,25 +15,25 @@ func f(from string) {
func main() { func main() {
// Suppose we have a function call `f(s)`. Here's how // Предположим, у нас есть вызов функции `f(s)`. Вот
// we'd call that in the usual way, running it // как мы бы вызвали её обычным способом, запустив
// synchronously. // синхронно.
f("direct") f("direct")
// To invoke this function in a goroutine, use // Чтобы вызвать эту функцию в горутине, используйте
// `go f(s)`. This new goroutine will execute // `go f(s)`. Эта новая горутина будет выполняться
// concurrently with the calling one. // одновременно с вызывающей фукнцией.
go f("goroutine") go f("goroutine")
// You can also start a goroutine for an anonymous // Вы так же можете вызывать анонимные функции в
// function call. // горутнах.
go func(msg string) { go func(msg string) {
fmt.Println(msg) fmt.Println(msg)
}("going") }("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) time.Sleep(time.Second)
fmt.Println("done") 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 $ go run goroutines.go
direct : 0 direct : 0
direct : 1 direct : 1
@ -12,5 +12,5 @@ goroutine : 1
goroutine : 2 goroutine : 2
done done
# Next we'll look at a complement to goroutines in # Далее мы рассмотрим дополнение к горутинам в
# concurrent Go programs: channels. # параллельных программах Go: каналы.