Hi guys,
I am learning golang with the fantastic book "Go in Action". And I have
some problems with understanding when to switch a goroutine to be run in
chapter 6.
package main
1
2 import (
3 "fmt"
4 "runtime"
5 "sync"
6 )
7
8 func main() {
9 runtime.GOMAXPROCS(1)
10
11 var wg sync.WaitGroup
12 wg.Add(2)
13
// first goroutine
14 go func() {
15 defer wg.Done()
16 for c := 0; c < 3; c++ {
17 for char := 'a'; char < 'a'+26; char++ {
18 fmt.Printf("%c", char)
19 }
20 }
21 fmt.Println("")
22 }()
23
// second goroutine
24 go func() {
25 defer wg.Done()
26 for c := 0; c < 3; c++ {
27 for char := 'A'; char < 'A'+26; char++ {
28 fmt.Printf("%c", char)
29 }
30 }
31 fmt.Println("")
32 }()
33
34 wg.Wait()
}
Why the result is printing uppercase letters first?
I mean the two goroutines are passed in a waiting queue in order of the
time they were generated, and they were meant to be executed in the same
order.
So why the second goroutine got executed first?
Or is the fmt.Println method is IO-related?
Please help me understand this situation.
Thanks!
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.