package context import ( "context" "fmt" "google.golang.org/grpc/metadata" ) func AppendToContext(ctx context.Context, key string, value interface{}) context.Context { ctx = context.WithValue(ctx, key, value) _value := "" switch value.(type) { case []byte: _value = string(value.([]byte)) break case string: _value = value.(string) break default: _value = fmt.Sprint(value) break } md, ok := metadata.FromOutgoingContext(ctx) if !ok { md = make(map[string][]string) md.Set(key, _value) ctx = metadata.NewOutgoingContext(ctx, md) return ctx } md.Set(key, _value) ctx = metadata.NewOutgoingContext(ctx, md) return ctx }