【发布时间】:2022-01-02 15:25:17
【问题描述】:
package main
import (
"fmt"
)
type A struct{
exit chan bool
}
func (a *A) f(){
select{
//the routine process
//quit
case <- a.exit:
fmt.Println("-----over-----")
a.exit <- true
fmt.Println("+++++over++++++")
}
}
func main() {
a := A{}
go a.f()
a.exit = make(chan bool)
a.exit <- true
}
我想运行多个 goroutine,我想让 main func 注意到其他 goroutine 退出。 这是我的代码,但是选择中的程序块,程序只输出“-----over-----”,没有“+++++over++++++”,代码有什么问题?感谢您帮助。
【问题讨论】:
-
把“a.exit = make(chan bool)”改成“a.exit = make(chan bool,1)”就好了