When the program exits, all goroutines die instantly. The doc is saying
that the program does not wait for any goroutine to complete except the
"main" goroutine (the one that invoked the function main at startup). If
you do want to wait for other goroutines to exit before exiting the
program, which is often something you do want, that can be done using
sync.WaitGroup or other synchronization tools like channels. In the case of
Tamas's goroutine, nothing in main waits for it, assuming the program isn't
sent an interrupt signal, so the goroutine never "completes". It does
"exit" though, because all goroutines are part of the program, and when the
program exits, it exits entirely. Any goroutine that was running as part of
the program isn't running anymore, and in fact doesn't even "exist" in any
meaningful sense anymore.

You are correct that contexts can be used to inform goroutines when to
perform cleanup steps and/or exit cleanly, but if you want to ensure that
the cleanup actually completes, you need to wait for the goroutine to
finish before returning from main (or running os.Exit, log.Fatal, panicking
without recovering, etc.). Otherwise the goroutine may abruptly stop
running at any time as the program exits.

I hope that helps to clarify the documentation for you.

On Wed, Jan 24, 2018 at 11:03 PM Sundar Nadathur <ns1.sun...@gmail.com>
wrote:

> I am probably mis-understanding or missing something. The golang spec
> <https://golang.org/ref/spec#Program_execution> says:
>     Program execution begins by initializing the main package and then
> invoking the function main. When that function invocation returns, the
> program exits. *It does not wait for other (non-main) goroutines to
> complete*.
>
> I gather that the context mechanism addresses this issue, by requiring
> every relevant go routine to get a context as a parameter and to check for
> ctx.Done channel. Since calling cancel() would send a message through that
> channel, every goroutine that is designed to listen on that channel would
> 'get the message' and can do their own cleanup/exit.
>
> However, this specific goroutine does not receive the context. The golang
> spec seems to say that it is not guaranteed to exit when the app dies or
> exits. Please clarify.
>
> Regards,
> Sundar
>
>
> On Wednesday, January 24, 2018 at 12:05:35 PM UTC-8, Tamás Gulácsi wrote:
>>
>> if the main goroutine exits, all other goroutines exit, too.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to