Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context awareness #323

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add context awareness #323

wants to merge 2 commits into from

Conversation

dkotik
Copy link

@dkotik dkotik commented Feb 27, 2024

It is expected for Golang APIs to be context-aware by default. I assume many projects are relying on context to pass a localizer down the call stack as I am doing. It makes sense to scope the context key to the same package.

I added two functions:

  • ContextWithLocalizer to insert
  • LocalizerFromContext to recover

@nicksnyder
Copy link
Owner

Can you be more specific why you need this? It seems like you can pass around a Localizer however you want in your project, but I don't see the benefit of having this as part of the public API.

@dkotik
Copy link
Author

dkotik commented Mar 4, 2024

@nicksnyder I don't really need this. I just noticed that if you try to localize all the error messages in a project, not just the templates, you are likely to encounter a situation where you have to pass the localizer through the context.

At that point you have to create a new context key in that project and accessors for it.

The idea is that mature projects will do this anyway sooner or later. Having it in the package itself may save humanity some cumulative work hours over the long term.

@dkotik
Copy link
Author

dkotik commented Mar 27, 2024

@nicksnyder I had another situation come up today that warrants this. I am internationalizing a library, which will be used in an internationalized webservice. Both of those will have a context get/set pair, redundantly.

@slsyy
Copy link

slsyy commented May 15, 2024

It is expected for Golang APIs to be context-aware by default

Context-aware means that you receive ctx in a function/method params and you support cancellation based on that ctx value. In case of that library it does not make sense as there is no IO nor operations are slow.

FromContext, WithContext is for sure useful, but it has nothing in common with the context awareness

@dkotik
Copy link
Author

dkotik commented May 15, 2024

FromContext, WithContext is for sure useful, but it has nothing in common with the context awareness

@slsyy By itself certainly not, but those functions would greese the implementation of context awareness when the library is used.

@nicksnyder Jonathan Amsterdam was adamant about having ContextWithLogger and LoggerFromContext methods for the standard library log/slog package. He folded to peer pressure and they were removed, but he openly stated last Gophercon that he still thinks it was a mistake not to include them. I think he is on to something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants