This example demonstrates how to use Unmarshal to retrieve metadata into an arbitrary type.
()
| 11 | // This example demonstrates how to use Unmarshal to retrieve metadata into an |
| 12 | // arbitrary type. |
| 13 | func 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 | } |