Исходники и презентации
This commit is contained in:
29
lessons/contexts/context_with_value/main.go
Normal file
29
lessons/contexts/context_with_value/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
traceCtx := context.WithValue(context.Background(), "trace_id", "12-21-33")
|
||||
makeRequest(traceCtx)
|
||||
|
||||
oldValue, ok := traceCtx.Value("trace_id").(string)
|
||||
if ok {
|
||||
fmt.Println("mainValue", oldValue)
|
||||
}
|
||||
}
|
||||
|
||||
func makeRequest(ctx context.Context) {
|
||||
oldValue, ok := ctx.Value("trace_id").(string)
|
||||
if ok {
|
||||
fmt.Println("oldValue", oldValue)
|
||||
}
|
||||
|
||||
newCtx := context.WithValue(ctx, "trace_id", "22-22-22")
|
||||
newValue, ok := newCtx.Value("trace_id").(string)
|
||||
if ok {
|
||||
fmt.Println("newValue", newValue)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user