Created
December 27, 2021 01:06
-
-
Save armando-couto/ecc1b9dd9d5af66cd159309570673285 to your computer and use it in GitHub Desktop.
Goroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
letter, number := make(chan bool), make(chan bool) | |
wg := sync.WaitGroup{} | |
go func() { | |
for ch := 'A'; ch < 'Z'; ch += 1 { | |
letter <- true | |
fmt.Print(string(ch)) | |
<-number | |
} | |
close(letter) | |
}() | |
wg.Add(1) | |
go func() { | |
start := 1 | |
for { | |
number <- true | |
_, ok := <-letter | |
if ok == false { | |
break | |
} | |
fmt.Print(start) | |
start += 1 | |
} | |
wg.Done() | |
}() | |
<-number | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment