Welcome! Let's learn and talk about the context package, specifically about how to implement deadlines, cancellation and request-scoped values using the functions defined in the package: * context.WithDeadline and context.WithTimeout * context.WithCancel and * context.WithValues The code is available here: 🤍github.com/MarioCarrion/videos/tree/563b41660420a5e77d25157f1d4798f343d12d22/2021/05/07 * Blog: 🤍mariocarrion.com/2021/05/31/learning-golang-context-package.html - * Previous Episode: "Golang Microservices: Events and Background Jobs using RabbitMQ": 🤍youtu.be/L0yJxCKrkIY * Playlist "Building Microservices in Go/Golang": 🤍🤍youtube.com/playlist?list=PL7yAAGMOat_Fn8sAXIk0WyBfK_sT1pohu * Playlist "Golang Tools and Packages": 🤍🤍youtube.com/playlist?list=PL7yAAGMOat_HEEOvH99agDs_5g51A0Ls3 * Playlist "Testing in Go": 🤍🤍youtube.com/playlist?list=PL7yAAGMOat_HSeW4zF0uRL9EaHadE4ZZq 00:00 Intro 00:56 What is the context package? 01:19 Context Deadlines 04:06 Cancellation Signals 07:32 Request-scoped values 10:02 Best practices 11:17 Parting words - Who am I: Hello👋🏼! I'm Mario, a Hands-on Software Architect and Lead Backend Engineer with more than 16 years of professional experience building all kinds of software including on-premise Industrial Automation Systems, Linux Accessibility Desktop and Browser Components as well as Distributed Advertising Microservices. Every week I will share with you different topics I've learned while working for small startups and large companies including the processes I've followed over the years for successfully delivering complex enterprise systems from start to end. Subscribe if you like Software Development, Software Architecture and Systems Design! Keep it up. Don't give up! #golang #microservices #programming - Our affiliate links below Shop our favorite products → 🤍🤍amazon.com/shop/rubycarrion Vlogging setup → 🤍rubycarrion.com/camerasforvlogging/ Get a 30 day FREE trial of Epidemic Sound → 🤍🤍epidemicsound.com/referral/szx441/ Try Amazon Prime 30-Day FREE trial → 🤍amzn.to/3s0el1R I love getting Cash Back and think you will too! Join for free and get $20 when you spend $20 → 🤍🤍rakuten.com/r/RUBYRA132?eeid=28187 Sign up to start using vidIQ to grow your YouTube channel for FREE → 🤍vidiq.com/rubycarrion - Our Vlog Channel 🤍🤍youtube.com/c/RubyCarrion/videos
I like your examples. They are very clear and to the point.
Amigo sos argentino? tu inglés me suena muy argentino jajaj
Thank you for this class. Finally I understood what context is and how to work with them. No, it is not dark sorcery...
the video image is too poor, you need to fix it more
Great, a side question, what is the theme you are using at your terminal?
Thanks for making such great videos, they have helped me in my current job with working with golang. I come from JS / Ruby land and typed languages can be quite confusing but I see the appeal. Your videos are great for me personally and I have learned a lot and applied it to my career. It's really great to see someone who has such passion for their work, I hope to be like that one day, since I'm pretty junior, I admire those who can "think quickly" and come up with solutions, I hope to be that kind of person one day.
Do you have to use select-case syntax for this, or are there other options?
Here's an example I have in mind: func main creates context with deadline, like in the example, then creates anonymous function (which does some job, for example waits 1 second) and calls it with goroutine, then blocks at ctx.Done() (assuming it's blocking operation), waiting for deadline (for example 2 seconds). How can I make the anonymous function mark that the task has been completed? Should I call cancel()? What happens to the "defer cancel()" in such scenario?
I think that the explanation for WithDeadline and WithTimeout in the example is overlapping (if that is proper term) - you showed how it works, but not what is the difference. The documentation says "WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).", and the way I understand is:
WithDeadline asks you for absolute timestamp when it should expire. It can be expressed as "it should be done by 2021-05-10" (specific date). It can be also calculated off something, like relatively to current time - "if it's not done in 10 minutes, I am leaving".
WithTimeout is abstraction or wrapper for WithDeadline, which covers common scenario of setting the deadline from current time, so you don't have to do "time.Now().Add(duration)" every time by hand - you only say how much time you are giving it (the "if it's not done in 10 minutes, I am leaving" scenario).
Is this correct?