去哪儿面试题 (Golang实现)多线程顺序输出1-75

FredricZhu ·
```go package main import ( "fmt" ) var ch = make(chan int) func print5(i int) { <-ch fmt.Println(i) } func main() { fmt.Println("Start print 75") for i := 1; i <= 75; i ++ { go print5(i) ch <- 1 } fmt.Println("End print 75") } ```
#4
更多评论
``` var a struct { Mu sync.RWMutex Count int } var wg sync.WaitGroup func main() { wg = sync.WaitGroup{} wg.Add(75) for i:=1; i<=75; i++ { go Add() } wg.Wait() } func Add() { defer wg.Done() a.Mu.Lock() a.Count++ fmt.Println(a.Count) a.Mu.Unlock() } ```
#1
输出结果不稳定,也就是说结果不对
#2