It depends.

If you know everything you have done is safely on disk somewhere, or you
have a stateless system, you can end the application by a panic.

However, in the event your program or requirements change, it can be
desirable to mix in lifecycle management early on. This includes graceful
shutdowns. Imagine, for instance, a system which loads a database into
memory and serves requests for clients. It's completely stateless, but you
might want to linger until all client requests are done before doing a
shutdown. This requires life-cycle management. In general, as software
grows, you can expect it to require more graceful handling of its
environment, so it might not be necessary right now, but it can be in the
future.

Periodically checking context.Done() is good form, because it makes your
go-routines cooperate with the rest of the program. The only thing that can
clean up is your goroutine, and that cleanup usually happens once the
context.Done() signal is given. As a rule of thumb: if there's a user
involved somewhere in the loop, you should think long and hard about a
quick panic before doing so.

On Wed, Apr 12, 2023 at 3:28 PM Frank <francisbfwb...@gmail.com> wrote:

> Hi Gophers,
>
> So I've run into a situation where my code would run for a rather long
> time (10minutes - ~1 hour). And I want to cancel it halfway sometimes. I
> know I can use channel/context can check for channel message to return
> early. But this requires explicitly checking for channel status and would
> introduce many messy code blocks. Is it feasible (and good) for me to
> listen to some signal and use panic to stop the execution once signal
> received?
>
> Thanks!
> Frank
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/d8bb51be-4f21-4658-93a9-d0e197b9bdden%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/d8bb51be-4f21-4658-93a9-d0e197b9bdden%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
J.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGrdgiXAiZnObaiGQ_uMwa%2BbRVodoQXoweS2KdnGpOkYizKj5w%40mail.gmail.com.

Reply via email to