Last active
March 11, 2020 05:39
-
-
Save rxrav/6d964e61634697d810d25c55b3b77db9 to your computer and use it in GitHub Desktop.
Example of WaitGroup in Golang
This file contains hidden or 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 sayHello() { | |
for i := 0; i <= 100; i++ { | |
fmt.Println("Hello", i) | |
} | |
} | |
func sayGoodBye() { | |
for i := 0; i <= 100; i++ { | |
fmt.Println("Good Bye", i) | |
} | |
} | |
func main() { | |
var wg sync.WaitGroup | |
fmt.Println("Execution start") | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
sayHello() | |
}() | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
sayGoodBye() | |
}() | |
wg.Wait() | |
fmt.Println("Execution end") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment