I am working with Go concurrency and have the following code:
package main
import (
"fmt"
"runtime"
"sync"
)
func main() {
runtime.GOMAXPROCS(1)
var wg sync.WaitGroup
wg.Add(2)
fmt.Println("Starting Goroutines")
go func() {
defer wg.Done()
for count := 0; count < 3; count++ {
for char := 'a'; char < 'a'+26; char++ {
fmt.Printf("%c", char)
}
}
fmt.Println()
}()
go func() {
defer wg.Done()
for count := 0; count < 3; count++ {
for char := 'A'; char < 'A'+26; char++ {
fmt.Printf("%c", char)
}
}
fmt.Println()
}()
fmt.Println("Waiting to Finish")
wg.Wait()
fmt.Println("Terminating")
}
My output is:
Starting Goroutines
Waiting to Finish
ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
Terminating
My problem is I declare the first goroutine to display lower case letters and the second goroutine to display upper case letters. Shouldn't the output be lowercase first then uppercase?
Any explanation would be helpful.
NOTE: This code came from the Go In Action ebook and i didnt fully understand their explanation.