The Go SDK is currently in experimental status. If you would like to provide feedback, please reach out to us with your suggestions and comments on our Discord.
Go - Kv.Keys()
Return an async iterable of keys in the store.
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
// Initialize the KV service
profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)
if err != nil {
return
}
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)
}
}
Parameters
- Name
prefix
- Optional
- Optional
- Type
- string
- Description
The prefix to filter keys by, if not provided all keys will be returned.
Examples
Get all keys from a key value store
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
// Initialize the KV service
profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)
if err != nil {
return
}
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)
}
}
Get keys filtered by prefix from a key value store
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/api/keyvalue"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
// Initialize the KV service
profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)
if err != nil {
return
}
// 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)
}
}