| 23 | ) |
| 24 | |
| 25 | func 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 | |
| 48 | func ExampleKeyExists() { |
| 49 | cli, err := clientv3.New(clientv3.Config{ |