LennyBoyatzis
4/14/2018 - 9:22 AM

waitgroup.go

package main

import (
	"fmt"
	"sync"
	"time"
)

func gobOne(wg *sync.WaitGroup) {
	time.Sleep(time.Second * 1)
	fmt.Println("I print 1st")
	wg.Done()
}

func gobTwo(wg *sync.WaitGroup) {
	time.Sleep(time.Second * 2)
	fmt.Println("I print 2nd")
	wg.Done()
}

func main() {
	var wg sync.WaitGroup

	wg.Add(2)
	go gobOne(&wg)
	go gobTwo(&wg)
	wg.Wait()

	/*
	Code below wg.Wait() will not run until
	goroutines added to the WaitGroup are completed
	*/
	fmt.Println("I print 3rd")
}