How To Use The Context Package In Golang?

preview_player
Показать описание

In this Golang tutorial, I will teach you how to use the context package for canceling slow-running tasks and how to share the state between functions having the same parent context.

#programming #golang
Рекомендации по теме
Комментарии
Автор

Context is like a bag or container that holds information that is shared between different parts of the program, especially when it comes to handling a request. This information can include things like timeouts, cancellation signals, and other data that is specific to that request.

For example, imagine you are building a web server that handles a lot of incoming requests. Each request has its own specific needs and requirements, such as a deadline for how long it should take to complete. The context allows you to keep track of these individual requirements for each request, and make sure that they are handled properly.

jondoe
Автор

I've been putting off learning context because of its strange nature. You've cleared it up in 17 minutes, thank you.

knj
Автор

I like how you're providing almost real life examples with such a passion! Your videos helped me A LOT!

vitiok
Автор

You don't actually need the for loop for the select. Since you don't have a default case, the select with just block until a response is received or the context is canceled.

metoh
Автор

I have just started learning Go, there are lots of vids with basics but this sort of content gives you a good taste of what you actually should learn to become a good golang dev. Thanks a lot for your hard work!

PavelBogart-cmfz
Автор

Keep the content coming man, following along the JSON API series to write my own project has helped me learn Go so fast its unreal

chunkspiggle
Автор

Simplest and Effective Tutorial for Golang Context package! Thank you so much!!

koteshahoskote
Автор

Thanks Anthony your videos from last few days are helping a lot man. 🍻

anshkumargarhewal
Автор

The function fetchData alone is a perfect description of why Go is such a beautiful language and how much effort it actually takes away from you. Image, you would have to code all of these implicit functionalities (including something complex like coroutines, thread communication and sync) by yourself or take them from some repo and try to incorporate it into your project, in C++ for instance. Here, you get all of that, wrapped up in a nice syntax that it easy to understand and follow. :)

nonlinearsound-
Автор

Surely the best explanation of contexts I've ever seen, thx

Programming-lrnb
Автор

Your channel is pure Gold! Thanks for sharing your knowledge with the world!

Marlem
Автор

Please note that the go-routine will not be cancelled, but will run until it's ended naturally (or the program exits). This is default Go behavior and as far as I know there's no way to cancel it from outside the go-routine.

sternparadis
Автор

amazing video. I would love to see more about tracing, loging and grafana

bambi
Автор

you are simply the best teacher i could ever have

joevtap
Автор

Why for-select instead of only select?

Suraj-tzoq
Автор

the best context tutorial I've ever seen!

parvizyuldashev
Автор

Great video, thanks so much! As for your remark about what language can do this so cleanly, I think C#'s CancellationToken implementation is as clean, or perhaps even cleaner as it can be passed to any task, and the cancellation token expiring can be handled by catching the exception thrown, which realistically is the only code you'd need to implement to get the equivalent of this code in Go.

aevitas_
Автор

Dude, that last application of a request ID in a context is the exact project I’m working on for work but in typescript. I wish we were making our APIs in GO but sadly not. I’ve been using a Axios wrapper inside a library I made and then in the request middleware I add the axios wrapper to an inversify config for dependencies injection. That way when ever a developer uses the axios wrapper in our code, all the classes have my axios wrapper with the request id as a public read only property. GO would be so much better

delduked
Автор

Thank you for your help! U r making a great job!

sargon
Автор

Appreciate your content! Thanks so much!

ManhPham-fhon