Original post

I’m wondering what is the best practice for spawning goroutine, whether to spawn at caller function or at callee function?

Spawning at caller function makes it explicit that it is background job/goroutine. But in some scenarios we need to spawn in callee function so to have better control over the goroutine (e.g., Channel/WaitGroup).

I want to know is there any guideline people follow to make this decision.

Example

type DataCollector struct {
  ...
}

func (d *DataCollector) Start() {
// alternatively goroutine can be spawned here
//  func() {
//   ...
// }
  ...
}

func main() {
  dataCollector := DataCollector{}
   dataCollector.Start()
  ...
}