Q: Goroutine creation design pattern When writing logic that should be run as part of a goroutine, should the method itself create the goroutine or the should that be the responsibility of the calling function? For example, which of the below would be preferable?
Creating the go routine within the method
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
}

Leaving it up to the caller
func longrunning() Result {
    // Business Logic Here
    return Result{}
}

func main() {
    c := make(chan Result)

    go func() {
        c <- longrunning()
    }()
    
    // Do additional tasks

    <-c
}


A: In Go it's trivial to launch a function as a goroutine (with the go keyword / statement), so always leave that option to the caller whether he/she wants to run your function synchronously or asynchronously.
That way you also relive yourself from delivering the result asynchronously, the client might already have a designated channel for that, "integrated" into other goroutines waiting for the result to process. Similarly, the client might already have a started goroutine preparing other things, and so the client can utilize that goroutine without having to launch another one if you "wire" that in.
