Quick actions

cmd+k|ctrl+k

Navigation

Languages

Concurrency / Wait for goroutines to finish / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:15:05Z

Updated

2019-03-26T10:15:05Z

package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup // 1

func routine(i int) {
	defer wg.Done() // 3
	fmt.Printf("routine %v finished\n", i)
}

func main() {
	wg.Add(10) // 2
	for i := 0; i < 10; i++ {
		go routine(i) // *
	}
	wg.Wait() // 4
	fmt.Println("main finished")
}
INFO