@kjk1/

gobook concurrency wait goroutine finish

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5803717361627849","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJzeW5jIgopCgovLyA6c2hvdyBzdGFydAp2YXIgd2cgc3luYy5XYWl0R3JvdXAgLy8gMQoKZnVuYyByb3V0aW5lKGkgaW50KSB7CglkZWZlciB3Zy5Eb25lKCkgLy8gMwoJZm10LlByaW50Zigicm91dGluZSAldiBmaW5pc2hlZFxuIiwgaSkKfQoKZnVuYyBtYWluKCkgewoJd2cuQWRkKDEwKSAvLyAyCglmb3IgaSA6PSAwOyBpIDwgMTA7IGkrKyB7CgkJZ28gcm91dGluZShpKSAvLyAqCgl9Cgl3Zy5XYWl0KCkgLy8gNAoJZm10LlByaW50bG4oIm1haW4gZmluaXNoZWQiKQp9CgovLyA6c2hvdyBlbmQ="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
	"fmt"
	"sync"
)

// :show start
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")
}

// :show end