MCPcopy
hub / github.com/etcd-io/etcd / ExampleKeyMissing

Function ExampleKeyMissing

client/v3/clientv3util/example_key_test.go:25–46  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23)
24
25func ExampleKeyMissing() {
26 cli, err := clientv3.New(clientv3.Config{
27 Endpoints: []string{"127.0.0.1:2379"},
28 })
29 if err != nil {
30 log.Fatal(err)
31 }
32 defer cli.Close()
33 kvc := clientv3.NewKV(cli)
34
35 // perform a put only if key is missing
36 // It is useful to do the check atomically to avoid overwriting
37 // the existing key which would generate potentially unwanted events,
38 // unless of course you wanted to do an overwrite no matter what.
39 _, err = kvc.Txn(context.Background()).
40 If(clientv3util.KeyMissing("purpleidea")).
41 Then(clientv3.OpPut("purpleidea", "hello world")).
42 Commit()
43 if err != nil {
44 log.Fatal(err)
45 }
46}
47
48func ExampleKeyExists() {
49 cli, err := clientv3.New(clientv3.Config{

Callers

nothing calls this directly

Calls 6

TxnMethod · 0.95
FatalMethod · 0.65
CloseMethod · 0.65
CommitMethod · 0.65
ThenMethod · 0.65
IfMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…