如何停止一个Goroutine
func TestStopGoroutine(t *testing.T) {
stopChan := make(chan struct{})
go func() {
for {
select {
case <-stopChan:
return
default:
println("goroutine is running")
time.Sleep(time.Second)
}
}
}()
time.Sleep(time.Second * 5)
close(stopChan)
fmt.Println("stop goroutine")
}