forked from mkaz/working-with-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22-goroutines.go
45 lines (36 loc) · 1.07 KB
/
22-goroutines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Originally from: https://gobyexample.com/channels
// [Reprinted under license](https://creativecommons.org/licenses/by/3.0/).
// Poetic changes were made.
package main
import (
"fmt"
"time"
)
func printSlowly(s string, n int) {
for i := 0; i < n; i++ {
fmt.Println(i, s)
time.Sleep(300 * time.Millisecond)
}
}
func main() {
// This is a normal function call.
// Main() will finish this off before continuing.
printSlowly("directly functioning", 3)
// The go functions below will each spin off to happen each in their own thread,
// Meaning they'll be called _concurrently_.
// Calling the named function as a go routine.
go printSlowly("red fish goroutine", 3)
go printSlowly("blue fish goroutine", 3)
// Call an anonymous function as a go routine.
go func(ss string, nn int) {
for i := 0; i < nn; i++ {
fmt.Println(i, ss)
time.Sleep(150 * time.Millisecond)
}
}("anony fish goroutine", 3)
// Waits for a button to be pushed.
// Try commenting this!
var input string
fmt.Scanln(&input) // Just push RETURN to finish the program.
fmt.Println("DONE.")
}