Skip to content
Santekno.com | Level Up Your Engineering Skills
EN
📖 0%
15 Jul 2023 · 3 min read ·Article 28 / 119
Go

Get to know Package Context With Value in Golang

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

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].

If we add a value to the context, it will automatically create a new child context and the initial context (original) will not be changed at all. And how to add the value, namely with the context.WithValue(parent, text) method.

Why should we use context?

context.WithValue is useful for storing information or data from the beginning when we create a process to the inside of the functions that we have defined so that until the last function we know what the value set in the initial function is like and has been passed to the function. -whichever function the context is running in.

Implementation and samples

Before going into the code, we need to first understand the diagram below.

context diagram

We will create a context by looking at the flow sequence in the diagram above. Making context A as a parent and having 2 child contexts, namely context B and context C. Then context B has more children, namely context D and E, but context C only has one child context, namely context F. So that you don’t get confused, let’s just try the code below.

go
 1func main() {
 2	contextA := context.Background()
 3
 4	contextB := context.WithValue(contextA, "b", "context B")
 5	contextC := context.WithValue(contextA, "c", "context C")
 6
 7	contextD := context.WithValue(contextB, "d", "context D")
 8	contextE := context.WithValue(contextB, "e", "context E")
 9
10	contextF := context.WithValue(contextC, "f", "context F")
11
12	fmt.Println(contextA)
13	fmt.Println(contextB)
14	fmt.Println(contextC)
15	fmt.Println(contextD)
16	fmt.Println(contextE)
17	fmt.Println(contextF)
18
19	fmt.Println(contextF.Value("f")) // dapat
20	fmt.Println(contextB.Value("c")) // dapat
21	fmt.Println(contextF.Value("c")) // dapat dari parent
22	fmt.Println(contextF.Value("b")) // tidak dapat
23	fmt.Println(contextB.Value("D")) // tidak dapat mengambil value child
24}
The results of the program when we run it will appear as below
bash
 1✗ go run app.go
 2context.Background
 3context.Background.WithValue(type string, val context B)
 4context.Background.WithValue(type string, val context C)
 5context.Background.WithValue(type string, val context B).WithValue(type string, val context D)
 6context.Background.WithValue(type string, val context B).WithValue(type string, val context E)
 7context.Background.WithValue(type string, val context C).WithValue(type string, val context F)
 8context F
 9<nil>
10context C
11<nil>
12<nil>

Conclusion

context can carry values from the parent to the lowest child so that whatever is below that we can use in the child context in the next process so that we don’t need to send any more inappropriate params, so in the child function we will capture it directly through the context only .

Related Articles

💬 Comments