example_watch_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. rch := cli.Watch(context.Background(), "foo")
  31. for wresp := range rch {
  32. for _, ev := range wresp.Events {
  33. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  34. }
  35. }
  36. // PUT "foo" : "bar"
  37. }
  38. func ExampleWatcher_watchPrefix() {
  39. cli, err := clientv3.New(clientv3.Config{
  40. Endpoints: endpoints,
  41. DialTimeout: dialTimeout,
  42. })
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. defer cli.Close()
  47. rch := cli.Watch(context.Background(), "foo", clientv3.WithPrefix())
  48. for wresp := range rch {
  49. for _, ev := range wresp.Events {
  50. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  51. }
  52. }
  53. // PUT "foo1" : "bar"
  54. }