#Context

4 articles

Recognizing Package Context With Timeout In Golang

Recognizing Package Context With Timeout In Golang

Package introduction context.WithTimeout

In the previous article we learned context.WithCancel where we manually send the cancel signal from context. But we can also add cancel signal to context automatically by using timeout timings. By using timeout, we no longer need to manually call cancel execution, but with timeout cancel will be automatically executed if the timeout time has passed.

Getting to Know Package Context With Cancel On Golang

Getting to Know Package Context With Cancel On Golang

Package introduction context.WithCancel

Context which can add values, we can also add cancel signals to the context. Usually context cancel is used when we need to run another process, and we want to cancel the process. This context cancel is usually run using a different goroutine so that we easily want to cancel the execution of the goroutine, so we just send a cancel signal to the context then the goroutine we want to stop can be done.

Get to know Package Context With Value in Golang

Get to know Package Context With Value in Golang

Introduction to the context.WithValue package

At the beginning of the context explanation, we know that the context will be created for the first time during initialization using context.Background() or context.TODO(), where the context does not have a value, aka it is still empty. We can add values from a context with a concept like a map, namely [key - value].

Getting to Know Package Context in Golang

Getting to Know Package Context in Golang

Introduction to package context

Context is a package that can store and carry data values, timeout signals and deadline signals. This context runs and is created per request. The context usually carries the value from initialization until the end of the process. This context is sometimes a necessity when we code a program so that from the beginning of the function to the next function the previous value can be used if the process in the next function requires data from the initial process.