Learning Golang Context!! Never Looked At It!
Вставка
- Опубліковано 1 лют 2024
- LIVE ON TWITCH: / theprimeagen
Become a backend engineer. Its my favorite site
boot.dev/?promo=PRIMEYT
This is also the best way to support me is to support yourself becoming a better backend engineer.
Get in on Discord: / discord
Get in on Twitter: / theprimeagen
Got Something For Me to Read or Watch??:
/ theprimeagenreact - Наука та технологія
Hey Prime, we would love more of your takes on advanced Go topics.
Thank you for your hard work!
I find that context as a kv store tends to get messy when it's really common in a codebase.
This has perf overhead (contexts are trees, and key lookups are traversals) but worse it can lead to hidden side effects if a function differs it behaviour based on contextual information.
It can be a lifesaver if you need to have a globally accessible request scoped value, but use with caution.
Prime, ninja edition
Will he update the thumbnails when he returns to normal… hmmm
lol
Loving this Go content lately, adding all of them to downloads to binge watch 💯
Started learning go a week ago as a experienced developer and boy oh boy do I already love it with all my heart. It's all so easyyy and simple and straight to the point
Prime, using middleware to put things into a context is a super cool pattern.
Let’s say you have the following route `/foo/:fooID/bar/:barID`. You could have a middleware package maintain a cache of the `foo` resource by its `fooID`. The middleware can pull the `fooID` from the path, and put the `foo` resource into the request context. This means that `bar`s handler doesn’t need to know the specifics of how to fetch `foo`, it just pulls it out of the context.
If we add another route like `/foo/:fooID/grug/:grugID`, the ‘grug’ handler will also have access to the `foo` resource.
As routes continue to be added under `foo`, you save yourself the trouble of putting in the boilerplate code to go fetch it.
That's super interesting. Any code examples?
I initially liked the approach, but mapping all values to `interface{}` within the context package of Go posed challenges due to its generic nature. To address this, I now define a struct with fields that are consistently required across different middleware handlers. This struct is passed as a parameter to handler functions, which improves type safety and enables syntax highlighting, making the code more maintainable. I remain open to other solutions that may be more efficient though.
Love the learing with the Primeagen , would love to see more of this
Context is amazing. Watching you learn Go is super awesome, I’ve been with it for almost 10 years and it just now feels like Go is getting its due.
bryan your videos are awesome too. i remember there was so little good go content a couple of years go and I stumbled on your videos. I was starting a new job and your videos helped me ramp up. thank you.
Prime's brain is transparent now
I do a DB lookup of a user by his token and then I put all the necessary info into context (key, value) and use it across the functions which have access to the request. It's just something you share data across functions within one request.
No different from a React context, where you share data across components.
His hair is the same colour as the text string highlights. lol
17:28 real Dad moment showing through right there
The cast can not blow up because it’s one with two assignments (There’s probably a proper name for this): the casted value and ok (bool indicating if the cast was successful). This is even mentioned in the comment above in the example code.
we can see through your brain .😂😂
Children cannot cancel the parent but they can cancel their own children's context derived from them. I have seen open telemetry using context in a great way
C# (sorry for the curse word), as said earlier, has it as 2 separate concepts: CancellationToken and HttpContext
1)CT just basically passed inside of any async method, and internal method should manually check, when running continuous operation, is token revoked, and stop in this case.
2)HC is basically storage for all request-scoped data, e.g. user identity, headers, request and response data, and it's shared between chains of async called method (because async in c# works as a state machine, so diff. threads, that work on 1 chain, should somehow now, how to have contrxt of request)
C# MENTIONED
IIRC there is no propagation of cancellations in C# (except manual)
Didn't think about the green screen when you got that hair, did you? :D
Gen this is similar to C#' ish Cancellation tokens.
"vim is crap if you want it to be like vscode"
Nice hair
How did he make the page display in dark mode?!
DarkReader, probably
switchHairColorWithMustache()
Nice transparent hair.
Probably can whip out a context like thing in Rust in an evening
(I use Rust btw)
This talk explains context incredibly well imo: ua-cam.com/video/mfgBhGu5pco/v-deo.html
balding