khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2015 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | /* |
| 16 | Package client provides bindings for the etcd APIs. |
| 17 | |
| 18 | Create a Config and exchange it for a Client: |
| 19 | |
| 20 | import ( |
| 21 | "net/http" |
| 22 | "context" |
| 23 | |
| 24 | "go.etcd.io/etcd/client" |
| 25 | ) |
| 26 | |
| 27 | cfg := client.Config{ |
| 28 | Endpoints: []string{"http://127.0.0.1:2379"}, |
| 29 | Transport: DefaultTransport, |
| 30 | } |
| 31 | |
| 32 | c, err := client.New(cfg) |
| 33 | if err != nil { |
| 34 | // handle error |
| 35 | } |
| 36 | |
| 37 | Clients are safe for concurrent use by multiple goroutines. |
| 38 | |
| 39 | Create a KeysAPI using the Client, then use it to interact with etcd: |
| 40 | |
| 41 | kAPI := client.NewKeysAPI(c) |
| 42 | |
| 43 | // create a new key /foo with the value "bar" |
| 44 | _, err = kAPI.Create(context.Background(), "/foo", "bar") |
| 45 | if err != nil { |
| 46 | // handle error |
| 47 | } |
| 48 | |
| 49 | // delete the newly created key only if the value is still "bar" |
| 50 | _, err = kAPI.Delete(context.Background(), "/foo", &DeleteOptions{PrevValue: "bar"}) |
| 51 | if err != nil { |
| 52 | // handle error |
| 53 | } |
| 54 | |
| 55 | Use a custom context to set timeouts on your operations: |
| 56 | |
| 57 | import "time" |
| 58 | |
| 59 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 60 | defer cancel() |
| 61 | |
| 62 | // set a new key, ignoring its previous state |
| 63 | _, err := kAPI.Set(ctx, "/ping", "pong", nil) |
| 64 | if err != nil { |
| 65 | if err == context.DeadlineExceeded { |
| 66 | // request took longer than 5s |
| 67 | } else { |
| 68 | // handle error |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | */ |
| 73 | package client |