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 serviceprofiles := 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 storefor {key, err := keys.Recv()if err != nil {// check if the stream has endedbreak}// do something with the keyfmt.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 serviceprofiles := 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 storefor {key, err := keys.Recv()if err != nil {// check if the stream has endedbreak}// do something with the keyfmt.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 serviceprofiles := nitric.NewKv("profiles").Allow(keyvalue.KvStoreGet, keyvalue.KvStoreSet, keyvalue.KvStoreDelete)// make function with keyvalue.ScanKeysOption typekeys, 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 storefor {key, err := keys.Recv()if err != nil {// check if the stream has endedbreak}// do something with the keyfmt.Printf("Key: %s\n", key)}nitric.Run()}
Last updated on Dec 19, 2024