Back to godoc.org
google.golang.org/grpc / attributes

Package attributes

v1.31.0
Latest Go to latest

The latest major version is .

Published: Jul 30, 2020 | License: Apache-2.0 | Module: google.golang.org/grpc

Overview

Package attributes defines a generic key/value store used in various gRPC components.

All APIs in this package are EXPERIMENTAL.

Index

Examples

type Attributes

type Attributes struct {
	// contains filtered or unexported fields
}

Attributes is an immutable struct for storing and retrieving generic key/value pairs. Keys must be hashable, and users should define their own types for keys.

Example

Code:

package main

import (
	"fmt"
	"google.golang.org/grpc/attributes"
)

func main() {
	type keyOne struct{}
	type keyTwo struct{}
	a := attributes.New(keyOne{}, 1, keyTwo{}, "two")
	fmt.Println("Key one:", a.Value(keyOne{}))
	fmt.Println("Key two:", a.Value(keyTwo{}))
}
Key one: 1
Key two: two

func New

func New(kvs ...interface{}) *Attributes

New returns a new Attributes containing all key/value pairs in kvs. If the same key appears multiple times, the last value overwrites all previous values for that key. Panics if len(kvs) is not even.

func (*Attributes) Value

func (a *Attributes) Value(key interface{}) interface{}

Value returns the value associated with these attributes for key, or nil if no value is associated with key.

func (*Attributes) WithValues

func (a *Attributes) WithValues(kvs ...interface{}) *Attributes

WithValues returns a new Attributes containing all key/value pairs in a and kvs. Panics if len(kvs) is not even. If the same key appears multiple times, the last value overwrites all previous values for that key. To remove an existing key, use a nil value.

Example

Code:

package main

import (
	"fmt"
	"google.golang.org/grpc/attributes"
)

func main() {
	type keyOne struct{}
	type keyTwo struct{}
	a := attributes.New(keyOne{}, 1)
	a = a.WithValues(keyTwo{}, "two")
	fmt.Println("Key one:", a.Value(keyOne{}))
	fmt.Println("Key two:", a.Value(keyTwo{}))
}
Key one: 1
Key two: two

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier