Returning from a goroutine which calls another goroutine

Issue

I wonder what happens to goroutines called from another goroutine when it returns. Do they continue running or get terminated?
Here is a sample code to show what I mean:

func func() {
    // Doing something
}

func func2() {
        go func()
        // Doing things that end with a return
    }

func main() {
    go func2()
}

what happens to func() goroutine when func2() goroutine returns?

Solution

You can make an experiment on excellent https://play.golang.org playground!
I recommend experiment there – it helps very much with golang studying.

https://play.golang.org/p/OJ9sgakV-S

I’ve added 2 second delay, because if program exit too quickly, you ‘ll not see results from goroutines.

Answered By – Eugene Lisitsky

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.