1:for
//使用无线循环
reutrn
}
例如:启动三个 goroutine 等待三个 goroutine 执行结束一下 退出主 goroutine
var c bool = false
var nums int = 0
for i := 0; i < 3; i++ {
go func() {
fmt.Println("begin------------end")
time.Sleep(10 * time.Second)
nums++
if nums == 2 {
c = true
}
}()
}
for {
if c == true {
return
}
}
2:chan
var c = make(chan bool)
........
.......
<- c
如果想退出主进程 则
c <- true
例如:启动三个 goroutine 等待三个 goroutine 执行结束一下 退出主 goroutine
var c = make(chan bool)
var nums int = 0
for i := 0; i < 3; i++ {
go func() {
fmt.Println("begin------------")
time.Sleep(10 * time.Second)
nums++
if nums == 2 {
c <- true
}
}()
}
<-c
//使用无线循环
for{
}
如果想退出
reutrn
}
例如:启动三个 goroutine 等待三个 goroutine 执行结束一下 退出主 goroutine
var c bool = false
var nums int = 0
for i := 0; i < 3; i++ {
go func() {
fmt.Println("begin------------end")
time.Sleep(10 * time.Second)
nums++
if nums == 2 {
c = true
}
}()
}
for {
if c == true {
return
}
}
2:chan
var c = make(chan bool)
........
.......
<- c
如果想退出主进程 则
c <- true
例如:启动三个 goroutine 等待三个 goroutine 执行结束一下 退出主 goroutine
var c = make(chan bool)
var nums int = 0
for i := 0; i < 3; i++ {
go func() {
fmt.Println("begin------------")
time.Sleep(10 * time.Second)
nums++
if nums == 2 {
c <- true
}
}()
}
<-c