context.go 479 B

1234567891011121314151617181920212223242526
  1. package context
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/grpc/metadata"
  6. )
  7. func AppendToContext(ctx context.Context, key string, value interface{}) context.Context {
  8. ctx = context.WithValue(ctx, key, value)
  9. _value := ""
  10. switch value.(type) {
  11. case []byte:
  12. _value = string(value.([]byte))
  13. break
  14. case string:
  15. _value = value.(string)
  16. break
  17. default:
  18. _value = fmt.Sprint(value)
  19. break
  20. }
  21. ctx = metadata.AppendToOutgoingContext(ctx, key, _value)
  22. return ctx
  23. }