Skip to content

Instantly share code, notes, and snippets.

@rxrav
Last active March 11, 2020 05:39
Show Gist options
  • Save rxrav/6d964e61634697d810d25c55b3b77db9 to your computer and use it in GitHub Desktop.
Save rxrav/6d964e61634697d810d25c55b3b77db9 to your computer and use it in GitHub Desktop.
Example of WaitGroup in Golang
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