ctx := context.TODO() ctx = ContextWithFields(ctx, map[string]interface{}{ "userID": 1234, "action": "login", })
func doSomething(ctx context.Context) error { fields := GetFieldsFromContext(ctx) fields["status"] = "started" defer func() { fields["status"] = "completed" log.WithFields(fields).Info("doSomething completed") }() // ... }In this example, we define a function called "doSomething" that takes a context argument. First, we extract any fields that were previously added to the context using "GetFieldsFromContext". Then, we add a new field called "status" with the value "started". We use a `defer` statement to ensure that the "completed" status is logged when the function completes. The final log message includes all the fields currently in the context, as well as the message "doSomething completed". Overall, the "ContextWithFields" type in package library "github.com/codedellemc/libstorage/api/types" provides a convenient way to enhance a Go context with extra metadata that can be useful for logging and other types of analysis.