在编写应作为 goroutine 的一部分运行的逻辑时,方法本身应该创建 goroutine 还是应该由调用函数负责?例如,以下哪项更可取?
在方法中创建 go 例程
func longrunning() chan Result {
c := make(chan Result)
go func() {
// Business Logic Here
c <- Result{}
}()
return c
}
func main() {
c := longrunning()
// Do additional tasks
<-c
}
留给呼叫者
func longrunning() Result {
// Business Logic Here
return Result{}
}
func main() {
c := make(chan Result)
go func() {
c <- longrunning()
}()
// Do additional tasks
<-c
}
牧羊人nacy
相关分类