Adding context.Context as first parameter in every function

642 views
Skip to first unread message

Yash Bansal

unread,
Mar 1, 2024, 9:27:03 AM3/1/24
to golang-nuts
Hello,

I recently started writing code in Golang and wasn't aware that unlike Java, we can't use ThreadLocals to propagate a context across the code. It's mandatory to explicitly pass a context. I have already written a service which is live in production now, however for monitoring and other reasons, I need to pass the context argument to every function. 

ASK 
Is there a tool or library which can help me do the same, instead of me going to every function and it's caller and adding the context field?

But looks like people in this thread have abandoned the idea of making a library like this.

Any leads would be appreciated. 

::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachments are confidential and intended for the named recipient(s) only.E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted,lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents(with or without referred errors) shall therefore not attach any liability on the originator or redBus.com. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of redBus.com. Any form of reproduction, dissemination, copying, disclosure, modification,distribution and / or publication of this message without the prior written consent of authorized representative of redbus.com is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately.Before opening any email and/or attachments, please check them for viruses and other defects.

Axel Wagner

unread,
Mar 1, 2024, 10:21:16 AM3/1/24
to Yash Bansal, golang-nuts
Note that you generally don't want to pass `context.Context` to *every* function, but only to those, which actually make network calls or otherwise need to support cancellation. I'm not sure how a tool would go about deciding which ones those are.

--
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 on the web visit https://groups.google.com/d/msgid/golang-nuts/58b07900-05cf-4550-b069-96c6d4cc3614n%40googlegroups.com.

Sam Vilain

unread,
Mar 1, 2024, 12:37:34 PM3/1/24
to golan...@googlegroups.com

Hi Yash, unfortunately that's the only approach at the moment.

Your request is very apropos of my thread, "Could we trade all the `ctx context.Context` arguments for one pointer in `g`?".  This is the exact use case I have in mind.  I believe it should be possible, and even a fairly naïve implementation should not slow down code that does not use the context variables at all.

I have a proposal in draft at: https://github.com/samv/go-context-proposal

Cheers,
Sam

::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachments are confidential and intended for the named recipient(s) only.E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted,lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents(with or without referred errors) shall therefore not attach any liability on the originator or redBus.com. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of redBus.com. Any form of reproduction, dissemination, copying, disclosure, modification,distribution and / or publication of this message without the prior written consent of authorized representative of redbus.com is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately.Before opening any email and/or attachments, please check them for viruses and other defects.
--
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 on the web visit https://groups.google.com/d/msgid/golang-nuts/58b07900-05cf-4550-b069-96c6d4cc3614n%40googlegroups.com.
-- 
Sam

Yash Bansal

unread,
Mar 3, 2024, 11:26:16 PM3/3/24
to golang-nuts
@axel Most of the functions I have (except the utility ones) will be needing the context parameter, that's why I thought of adding context to every function, and just using auto-refactoring to remove context from the functions which doesn't use it.

@Sam Thanks for providing me with your repo link. Will go through it for sure. Sadly, I have started manually editing every function to add the parameter now. Let's hope the go-devs address my problem.
Reply all
Reply to author
Forward
0 new messages