Skip to content

Commit 4b87259

Browse files
committed
docs: add example in the readme
1 parent df4947e commit 4b87259

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

docs/examples.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@ import (
1919
)
2020

2121
func main() {
22-
addr := "localhost:8500"
22+
ctx := context.Background()
2323

24-
// Initialize a new store.
2524
config := &consul.Config{
2625
ConnectionTimeout: 10 * time.Second,
2726
}
2827

29-
kv, err := valkeyrie.NewStore(consul.StoreName, []string{addr}, config)
28+
kv, err := valkeyrie.NewStore(ctx, consul.StoreName, []string{"localhost:8500"}, config)
3029
if err != nil {
3130
log.Fatal("Cannot create store consul")
3231
}
3332

3433
key := "foo"
35-
ctx := context.Background()
3634

3735
err = kv.Put(ctx, key, []byte("bar"), nil)
3836
if err != nil {

readme.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,52 @@ The benefit of `valkeyrie` is not to duplicate the code for programs that should
1616

1717
You can refer to [Examples](https://github.com/kvtools/valkeyrie/blob/master/docs/examples.md) for a basic overview of the library.
1818

19+
```go
20+
package main
21+
22+
import (
23+
"context"
24+
"log"
25+
"time"
26+
27+
"github.com/kvtools/consul"
28+
"github.com/kvtools/valkeyrie"
29+
)
30+
31+
func main() {
32+
ctx := context.Background()
33+
34+
config := &consul.Config{
35+
ConnectionTimeout: 10 * time.Second,
36+
}
37+
38+
kv, err := valkeyrie.NewStore(ctx, consul.StoreName, []string{"localhost:8500"}, config)
39+
if err != nil {
40+
log.Fatal("Cannot create store consul")
41+
}
42+
43+
key := "foo"
44+
45+
46+
err = kv.Put(ctx, key, []byte("bar"), nil)
47+
if err != nil {
48+
log.Fatalf("Error trying to put value at key: %v", key)
49+
}
50+
51+
pair, err := kv.Get(ctx, key, nil)
52+
if err != nil {
53+
log.Fatalf("Error trying accessing value at key: %v", key)
54+
}
55+
56+
log.Printf("value: %s", string(pair.Value))
57+
58+
err = kv.Delete(ctx, key)
59+
if err != nil {
60+
log.Fatalf("Error trying to delete key %v", key)
61+
}
62+
}
63+
```
64+
1965
## Compatibility
2066

2167
A **storage backend** in `valkeyrie` implements (fully or partially) the [Store](https://github.com/kvtools/valkeyrie/blob/master/store/store.go#L69) interface.
@@ -44,7 +90,7 @@ The store implementations:
4490
- [redis](https://github.com/kvtools/redis)
4591
- [zookeeper](https://github.com/kvtools/zookeeper)
4692

47-
The store tempate:
93+
The store template:
4894

4995
- [template](https://github.com/kvtools/template)
5096

@@ -66,4 +112,4 @@ The [Maintainers](https://github.com/kvtools/valkeyrie/blob/master/maintainers.m
66112

67113
Apache License Version 2.0
68114

69-
Valkeyrie is a hard fork of the unmaintained [libkv](https://github.com/docker/libkv).
115+
Valkeyrie started as a hard fork of the unmaintained [libkv](https://github.com/docker/libkv).

0 commit comments

Comments
 (0)