Created
March 11, 2023 15:22
-
-
Save Glyphack/157dfabe23bc28d92f91a43ad551ce95 to your computer and use it in GitHub Desktop.
Golang: Wait group, channels, blocking operation
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() { | |
ch := make(chan int) | |
wg := sync.WaitGroup{} | |
go func() { | |
for i := range ch { | |
fmt.Println(i) | |
fmt.Println("call") | |
wg.Done() | |
} | |
fmt.Println("exit") | |
wg.Done() | |
}() | |
wg.Add(3) | |
ch <- 1 | |
ch <- 2 | |
close(ch) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment