1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package main
import ( "context" "fmt" "go.etcd.io/etcd/clientv3" "time" )
func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints:[]string{"localhost:2379"}, DialTimeout: 5 * time.Second, })
if err != nil{ fmt.Println("conn err: ", err) return } defer cli.Close()
kv := clientv3.NewKV(cli)
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 5)
resp, err := kv.Put(ctx, "clientv3_key", "0507")
if err != nil{ fmt.Println("put err: ", err) } fmt.Println(resp)
getRsp, err := kv.Get(ctx, "clientv3_key") if err != nil { fmt.Println("get err:", err) return } fmt.Println(getRsp) rch := cli.Watch(context.Background(), "foo") for wresp := range rch { for _, ev := range wresp.Events { fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value) } } cancel() }
|