Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Go Language Go Language Overview Concurrency Goroutines

Why doesn't anything print out from the goroutine?

At around 02:19 in the video, nothing prints out, even though there are print statements in the longTask() function. Why is this? Please help! Thank you! :)

:dizzy: ~Alex :dizzy:

Steven Parker
Steven Parker
231,248 Points

:mailbox_with_mail: Hi, I got your request. But it looks like Jennifer's already got you covered.
Happy coding!

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Alexander Davison ! This was touched on just after this point in the video.

But you can see that our program's not producing any out put now. The problem is that as soon as the main go routine finishes, the program exits. So the other go routines don't get a chance to do anything.

And it's essentially this (as I understand it): these things are all happening simultaneously, The main routine asks the other routine to run 3 times, but once it asks them to do that... it exits completely. It takes longer for the routine that's printing to do its thing than it takes the main to say "Hey, run this 3 times, thanks". It's why a "sleep" was added later so that the program will wait until the others have finished.

Hope this helps! :sparkles:

Thanks! :grin:

Think about it like juggling. The normal way would throw one ball in the air. Wait for it to drop back (network response). Then throw the next ball in the air, wait for that.

With concurrency, we can throw multiple balls in the air, while waiting for the next one to drop. The more hands you have (cores) the more balls you can juggle.

Now for a more concrete explanation: The normal flow of operations would be linear: main(), THEN task()1 THEN task()2 ... etc.

However, with currency, all the tasks start together. So main(), task1(), task2(), ... taskN() all start about the same time.

The concurrent flow of operations would be: main() AND task1() AND task()2 ... etc.

Since main() is the first and main goroutine for the program when it stops, everything stops. If it stops before task1(), task2(), taskN(), then the program exits.

We will see how channels helps solves this problem in the next video.

Hope this helps!