package main import ( "context" "fmt" clientv3 "go.etcd.io/etcd/client/v3" "time" ) func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{"106.54.33.152:2379"}, DialTimeout: 5 * time.Second, }) if err != nil { panic(err) } defer cli.Close() ctx, cancel := context.WithCancel(context.Background()) defer cancel() _, err = cli.Put(ctx, "/services/service", "running") if err != nil { fmt.Printf("Failed to put service instance: %v\n", err) return } go func() { time.Sleep(time.Second * 5) commit, err := cli.Txn(ctx). If(clientv3.Compare(clientv3.Value("/services/service"), "=", "running")). Then(clientv3.OpPut("/services/service", "stop")). Else(clientv3.OpPut("/services/service", "error")). Commit() if err != nil { fmt.Printf("Failed to commit transaction: %v\n", err) return } if !commit.Succeeded { fmt.Printf("Transaction failed\n") } fmt.Println(commit) }() _, err = cli.Put(ctx, "/services/service", "running1") if err != nil { fmt.Printf("Failed to put service instance: %v\n", err) return } time.Sleep(time.Second * 10) }