SoFunction
Updated on 2025-03-05

grpc-go how to pass extra data through context

Use to read data from context

// ValueFromIncomingContext returns the metadata value corresponding to the metadata
// key from the incoming metadata if it exists. Key must be lower-case.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ValueFromIncomingContext(ctx , key string) []string {
	md, ok := (mdIncomingKey{}).(MD)
	if !ok {
		return nil
	}
	if v, ok := md[key]; ok {
		return copyOf(v)
	}
	for k, v := range md {
		// We need to manually convert all keys to lower case, because MD is a
		// map, and there's no guarantee that the MD attached to the context is
		// created using our helper functions.
		if (k) == key {
			return copyOf(v)
		}
	}
	return nil
}

Use the writing data to context

// AppendToOutgoingContext returns a new context with the provided kv merged
// with any existing metadata in the context. Please refer to the documentation
// of Pairs for a description of kv.
func AppendToOutgoingContext(ctx , kv ...string)  {
	if len(kv)%2 == 1 {
		panic(("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
	}
	md, _ := (mdOutgoingKey{}).(rawMD)
	added := make([][]string, len()+1)
	copy(added, )
	kvCopy := make([]string, 0, len(kv))
	for i := 0; i < len(kv); i += 2 {
		kvCopy = append(kvCopy, (kv[i]), kv[i+1])
	}
	added[len(added)-1] = kvCopy
	return (ctx, mdOutgoingKey{}, rawMD{md: , added: added})
}

metadata is built-in for grpc and is used to pass http header data to RPC services. It is divided into in and out. The corresponding keys are both an empty struct, namely: mdIncomingKey and mdOutgoingKey.

The server's ctx and md are printed out directly, as follows:

(ctx)
(md)
(type , val <not Stringer>).WithValue(type , val <not Stringer>).WithDeadline(2024-02-19 10:02:43.212614653 +0800 CST m=+41018.106555206 [1.999790196s]).WithValue(type , val <not Stringer>).WithValue(type , val <not Stringer>).WithValue(type , val <not Stringer>).WithValue(type , val <not Stringer>).WithValue(type , val <not Stringer>).WithCancel
map[:authority:[] append:[append-value] content-type:[application/grpc] extra:[extra-value] grpc-accept-encoding:[gzip] noncestr:[abc] signature:[0123456789] timestamp:[2021-07-01 00:00:00] traceparent:[00-89415f99d44e6f8f6e14e3fe8f13ad20-bf33b29c4362ca6a-00] user-agent:[grpc-go/1.59.0]]
signature: [0123456789]

Note that the value in md will be added with brackets "[]".

This is the article about grpc-go passing additional data through context. For more related grpc-go context, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!