import ( "github.com/coreos/go-etcd/etcd" "time" ) func main() { client := etcd.NewClient([]string{"http://localhost:4001"}) client.Watch("/foo", 0, true, nil, nil) for { select { case response := <-client.WatchResponse(): // handle key change event case <-time.After(time.Second * 10): // timeout after 10 seconds } } }
import ( "github.com/coreos/go-etcd/etcd" "fmt" ) func main() { client := etcd.NewClient([]string{"http://localhost:4001"}) response, _ := client.Watch("/", 0, true, nil, nil) for _, node := range response.Node.Nodes { fmt.Println(node.Key, node.Value) } }In this example, the client is watching the root directory of the Etcd cluster, and will receive a response that contains the current state of the directory. The client then iterates over the nodes in the directory and prints out the key-value pairs. Overall, the Watch package in go-etcd provides an easy way for clients to monitor changes to the Etcd cluster and keep their data in sync with the cluster.