Go - Kv.Keys()

Return an async iterable of keys in the store.

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/keyvalue"
)
func main() {
// Initialize the KV service
profiles := nitric.NewKv("profiles").Allow(keyvalue.KvStoreGet, keyvalue.KvStoreSet, keyvalue.KvStoreDelete)
keys, err := profiles.Keys(context.TODO())
if err != nil {
// handle error
}
// Get all keys from a key value store
for {
key, err := keys.Recv()
if err != nil {
// check if the stream has ended
break
}
// do something with the key
fmt.Printf("Key: %s\n", key)
}
nitric.Run()
}

Parameters

  • Name
    options
    Optional
    Optional
    Type
    ...ScanKeysOption
    Description

    Options for the scan keys operation. See below.

Scan keys options

  • Name
    WithPrefix(prefix)
    Optional
    Optional
    Type
    ScanKeysOption
    Description

    Filter keys by prefix.

Examples

Get all keys from a key value store

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/keyvalue"
)
func main() {
// Initialize the KV service
profiles := nitric.NewKv("profiles").Allow(keyvalue.KvStoreGet, keyvalue.KvStoreSet, keyvalue.KvStoreDelete)
keys, err := profiles.Keys(context.TODO())
if err != nil {
fmt.Println("Error getting keys: ", err)
return
}
// Get all keys from a key value store
for {
key, err := keys.Recv()
if err != nil {
// check if the stream has ended
break
}
// do something with the key
fmt.Printf("Key: %s\n", key)
}
nitric.Run()
}

Get keys filtered by prefix from a key value store

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/keyvalue"
)
func main() {
// Initialize the KV service
profiles := nitric.NewKv("profiles").Allow(keyvalue.KvStoreGet, keyvalue.KvStoreSet, keyvalue.KvStoreDelete)
// make function with keyvalue.ScanKeysOption type
keys, err := profiles.Keys(context.TODO(), keyvalue.WithPrefix("profile:"))
if err != nil {
fmt.Println("Error getting keys: ", err)
return
}
// Get all keys from a key value store
for {
key, err := keys.Recv()
if err != nil {
// check if the stream has ended
break
}
// do something with the key
fmt.Printf("Key: %s\n", key)
}
nitric.Run()
}
Last updated on Oct 16, 2024