Context cancellation and returned error

43 views
Skip to first unread message

Pierre Durand

unread,
Apr 3, 2026, 1:42:31 PM (19 hours ago) Apr 3
to golang-nuts
My code has a select with a case that waits for context cancellation.

Currently I'm doing this:
case <-ctx.Done():
return ctx.Err()

I noticed that some libraries and the stdlib are doing this:
case <-ctx.Done():
return context.Cause(ctx)

What is the best practice ?

Axel Wagner

unread,
12:17 AM (8 hours ago) 12:17 AM
to Pierre Durand, golang-nuts
The latter. ctx.Err() can only return the context-specific errors, while context.Cause can also return the error passed using WithCancelCause.

--
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...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/9adc5736-37da-4823-abc8-78f202171a15n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages