forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example3.go
62 lines (50 loc) · 1.34 KB
/
example3.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// https://play.golang.org/p/kz65m4PHmC
// Sample program to show how to create goroutines and
// how the goroutine scheduler behaves with two contexts.
package main
import (
"fmt"
"runtime"
"sync"
)
// init is called prior to main.
func init() {
// Allocate two logical processors for the scheduler to use.
runtime.GOMAXPROCS(2)
}
// main is the entry point for all Go programs.
func main() {
// wg is used to wait for the program to finish.
// Add a count of two, one for each goroutine.
var wg sync.WaitGroup
wg.Add(2)
fmt.Println("Start Goroutines")
// Declare an anonymous function and create a goroutine.
go func() {
// Display the alphabet three times.
for count := 0; count < 3; count++ {
for rune := 'a'; rune < 'a'+26; rune++ {
fmt.Printf("%c ", rune)
}
}
// Tell main we are done.
wg.Done()
}()
// Declare an anonymous function and create a goroutine.
go func() {
// Display the alphabet three times.
for count := 0; count < 3; count++ {
for rune := 'A'; rune < 'A'+26; rune++ {
fmt.Printf("%c ", rune)
}
}
// Tell main we are done.
wg.Done()
}()
// Wait for the goroutines to finish.
fmt.Println("Waiting To Finish")
wg.Wait()
fmt.Println("\nTerminating Program")
}