example_watch_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2016 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. package clientv3_test
  15. import (
  16. "fmt"
  17. "log"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. )
  21. func ExampleWatcher_watch() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. wc := clientv3.NewWatcher(cli)
  31. defer wc.Close()
  32. rch := wc.Watch(context.Background(), "foo", 0)
  33. for wresp := range rch {
  34. for _, ev := range wresp.Events {
  35. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  36. }
  37. }
  38. // PUT "foo" : "bar"
  39. }
  40. func ExampleWatcher_watchPrefix() {
  41. cli, err := clientv3.New(clientv3.Config{
  42. Endpoints: endpoints,
  43. DialTimeout: dialTimeout,
  44. })
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. defer cli.Close()
  49. wc := clientv3.NewWatcher(cli)
  50. defer wc.Close()
  51. rch := wc.WatchPrefix(context.Background(), "foo", 0)
  52. for wresp := range rch {
  53. for _, ev := range wresp.Events {
  54. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  55. }
  56. }
  57. // PUT "foo1" : "bar"
  58. }