Back to / clientv3 / namespace

Package namespace

Latest Go to latest

The latest major version is .

Published: May 20, 2020 | License: Apache-2.0 | Module:


Package namespace is a clientv3 wrapper that translates all keys to begin with a given prefix.

First, create a client:

cli, err := clientv3.New(clientv3.Config{Endpoints: []string{"localhost:2379"}})
if err != nil {
	// handle error!

Next, override the client interfaces:

unprefixedKV := cli.KV
cli.KV = namespace.NewKV(cli.KV, "my-prefix/")
cli.Watcher = namespace.NewWatcher(cli.Watcher, "my-prefix/")
cli.Lease = namespace.NewLease(cli.Lease, "my-prefix/")

Now calls using 'cli' will namespace / prefix all keys with "my-prefix/":

cli.Put(context.TODO(), "abc", "123")
resp, _ := unprefixedKV.Get(context.TODO(), "my-prefix/abc")
fmt.Printf("%s\n", resp.Kvs[0].Value)
// Output: 123
unprefixedKV.Put(context.TODO(), "my-prefix/abc", "456")
resp, _ = cli.Get("abc")
fmt.Printf("%s\n", resp.Kvs[0].Value)
// Output: 456


func NewKV

func NewKV(kv clientv3.KV, prefix string) clientv3.KV

NewKV wraps a KV instance so that all requests are prefixed with a given string.

func NewLease

func NewLease(l clientv3.Lease, prefix string) clientv3.Lease

NewLease wraps a Lease interface to filter for only keys with a prefix and remove that prefix when fetching attached keys through TimeToLive.

func NewWatcher

func NewWatcher(w clientv3.Watcher, prefix string) clientv3.Watcher

NewWatcher wraps a Watcher instance so that all Watch requests are prefixed with a given string and all Watch responses have the prefix removed.

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

Jump to identifier

Keyboard shortcuts

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