【发布时间】:2019-08-10 07:38:05
【问题描述】:
很快就会明白,我是一个 golang n00b。
我有一些基于事件通道启动 goroutines 的 go 代码。假设它启动了 2 个 goroutine,因为我们收到了 2 个 START 类型的事件。
goroutine 以 uri 作为参数开始,这给了我们一些独特的东西。
稍后我们收到一个 STOP 类型的事件。
如何停止以相同 uri 启动的 goroutine?
for {
select {
case event := <-eventCh:
if event.Entry != nil {
switch event.Action {
case foo.START:
log.Println("uri: ", event.Entry.URI)
go func(c chan []byte, u string) error{
//awesome goroutine code
}(myChan, event.Entry.URI)
case foo.STOP:
log.Println("uri: ", event.Entry.URI)
//I'd like to terminate the goroutine that matches event.Entry.URI
}
}
}
}
【问题讨论】: