Created
July 17, 2017 15:44
-
-
Save lclarkmichalek/bb7898ad075625e0657b7b4698e4f8e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"time" | |
"context" | |
etcd "github.com/coreos/etcd/clientv3" | |
"github.com/golang/glog" | |
) | |
func main() { | |
ctx := context.Background() | |
flag.Parse() | |
cli, err := etcd.New(etcd.Config{ | |
Endpoints: []string{"localhost:2379"}, | |
DialTimeout: 5 * time.Second, | |
}) | |
if err != nil { | |
glog.Exitf("failed to connect: %v", err) | |
} | |
defer cli.Close() | |
kvc := etcd.NewKV(cli) | |
_, err = kvc.Put(ctx, "test::c", "1") | |
if err != nil { | |
glog.Exitf("failed to put c: %v", err) | |
} | |
_, err = kvc.Txn(ctx).If( | |
etcd.Compare(etcd.Version("test::a"), ">", 0), | |
).Then( | |
etcd.OpPut("test::b", "1"), | |
).Else( | |
etcd.OpTxn([]etcd.Cmp{ | |
etcd.Compare(etcd.Value("test::c"), "=", "1"), | |
}, []etcd.Op{ | |
etcd.OpPut("test::d", "1"), | |
}, nil), | |
).Commit() | |
if err != nil { | |
glog.Exitf("failed to execute transaction: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment