doc.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. Package client provides bindings for the etcd APIs.
  16. Create a Config and exchange it for a Client:
  17. import (
  18. "net/http"
  19. "context"
  20. "go.etcd.io/etcd/client"
  21. )
  22. cfg := client.Config{
  23. Endpoints: []string{"http://127.0.0.1:2379"},
  24. Transport: DefaultTransport,
  25. }
  26. c, err := client.New(cfg)
  27. if err != nil {
  28. // handle error
  29. }
  30. Clients are safe for concurrent use by multiple goroutines.
  31. Create a KeysAPI using the Client, then use it to interact with etcd:
  32. kAPI := client.NewKeysAPI(c)
  33. // create a new key /foo with the value "bar"
  34. _, err = kAPI.Create(context.Background(), "/foo", "bar")
  35. if err != nil {
  36. // handle error
  37. }
  38. // delete the newly created key only if the value is still "bar"
  39. _, err = kAPI.Delete(context.Background(), "/foo", &DeleteOptions{PrevValue: "bar"})
  40. if err != nil {
  41. // handle error
  42. }
  43. Use a custom context to set timeouts on your operations:
  44. import "time"
  45. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  46. defer cancel()
  47. // set a new key, ignoring its previous state
  48. _, err := kAPI.Set(ctx, "/ping", "pong", nil)
  49. if err != nil {
  50. if err == context.DeadlineExceeded {
  51. // request took longer than 5s
  52. } else {
  53. // handle error
  54. }
  55. }
  56. */
  57. package client