MCPcopy Create free account
hub / github.com/DoNewsCode/core / ExampleBaggage_Unmarshal

Function ExampleBaggage_Unmarshal

ctxmeta/example_test.go:13–29  ·  view source on GitHub ↗

This example demonstrates how to use Unmarshal to retrieve metadata into an arbitrary type.

()

Source from the content-addressed store, hash-verified

11// This example demonstrates how to use Unmarshal to retrieve metadata into an
12// arbitrary type.
13func ExampleBaggage_Unmarshal() {
14 type DomainError struct {
15 Code int
16 Reason string
17 }
18
19 bag, _ := ctxmeta.Inject(context.Background())
20 derr := DomainError{Code: http.StatusTeapot, Reason: "Earl Gray exception"}
21 bag.Set("err", derr)
22
23 if target := (DomainError{}); bag.Unmarshal("err", &target) == nil {
24 fmt.Printf("DomainError Code=%d Reason=%q\n", target.Code, target.Reason)
25 }
26
27 // Output:
28 // DomainError Code=418 Reason="Earl Gray exception"
29}

Callers

nothing calls this directly

Calls 4

InjectFunction · 0.92
UnmarshalMethod · 0.65
SetMethod · 0.45
PrintfMethod · 0.45

Tested by

no test coverage detected