context.C and context.BG

145 views
Skip to first unread message

Alex Buchanan

unread,
Nov 17, 2017, 10:43:29 AM11/17/17
to golang-nuts
I don't have numbers, but context must be one of the most commonly used imports/types in my code. It shows up many function signatures. We try to use it for all cancelation.

Would it make sense to add an alias "C" to the type "Context"? And maybe "BG" for Background()? 

Also, is there a specific reason Background is a function and not a value?

Cheers,
Alex

m...@anmol.io

unread,
Nov 17, 2017, 9:25:41 PM11/17/17
to golang-nuts
I've found a hint. Apparently, both context.TODO and context.Background share a common implementation but they need distinct addresses.


So they cannot be constants because then they would not have an address. They cannot be variables because then you could modify them. Therefore the only solution left is to make them a function that returns a variable.

Then again, maybe you could make Background and TODO of an unexported type to prevent setting them. I think that was not chosen because it leaks the internals of the package.

But I'm not sure why they need distinct addresses in the first place. I'm guessing its for debugging or just what makes sense? context.TODO and context.Background are two different contexts and should be treated as such.

Sameer Ajmani

unread,
Nov 18, 2017, 9:01:07 AM11/18/17
to m...@anmol.io, golang-nuts
Context.TODO and Background have different String values; see emptyCtx.String.
--
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.
For more options, visit https://groups.google.com/d/optout.

Anmol Sethi

unread,
Nov 18, 2017, 9:24:55 AM11/18/17
to Sameer Ajmani, golang-nuts
Ah, thanks!

Alex Buchanan

unread,
Nov 18, 2017, 1:55:43 PM11/18/17
to golang-nuts
So, context.Bg could be a value then? context.TODO could stay a function.

Axel Wagner

unread,
Nov 18, 2017, 4:54:51 PM11/18/17
to Alex Buchanan, golang-nuts
Given that there should be exactly one call to context.Background per binary (in func main()), I don't understand the problem this is trying to solve. Similarly, context.TODO might be used, but should be used temporarily, as a migration tool, so it shouldn't matter that much.

(Regarding context.C, I don't have an opinion.)

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Alex Buchanan

unread,
Nov 18, 2017, 5:05:43 PM11/18/17
to golang-nuts

On Saturday, November 18, 2017 at 1:54:51 PM UTC-8, Axel Wagner wrote:
Given that there should be exactly one call to context.Background per binary (in func main()), I don't understand the problem this is trying to solve. Similarly, context.TODO might be used, but should be used temporarily, as a migration tool, so it shouldn't matter that much.

Fair point. context.C as a function argument everywhere was the impetus for the idea, context.Bg was an afterthought.

Also, that's not true in all my code. Maybe I'm doing it wrong, but I use Background a lot in tests and sometimes I use it with a timeout that is not tied to the parent context in order to, for example, give a routine 10 seconds to do cleanup/shutdown, or 10 seconds to make a client connection to a service.
Reply all
Reply to author
Forward
0 new messages