doc.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 CoreOS, Inc.
  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. "github.com/coreos/etcd/client"
  20. "golang.org/x/net/context"
  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. Create a KeysAPI using the Client, then use it to interact with etcd:
  31. kAPI := client.NewKeysAPI(c)
  32. // create a new key /foo with the value "bar"
  33. _, err = kAPI.Create(context.Background(), "/foo", "bar")
  34. if err != nil {
  35. // handle error
  36. }
  37. // delete the newly created key only if the value is still "bar"
  38. _, err = kAPI.Delete(context.Background(), "/foo", &DeleteOptions{PrevValue: "bar"})
  39. if err != nil {
  40. // handle error
  41. }
  42. Use a custom context to set timeouts on your operations:
  43. import "time"
  44. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  45. defer cancel()
  46. // set a new key, ignoring it's previous state
  47. _, err := kAPI.Set(ctx, "/ping", "pong", nil)
  48. if err != nil {
  49. if err == context.DeadlineExceeded {
  50. // request took longer than 5s
  51. } else {
  52. // handle error
  53. }
  54. }
  55. */
  56. package client