Skip to content

Instantly share code, notes, and snippets.

@lclarkmichalek
Created July 17, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lclarkmichalek/bb7898ad075625e0657b7b4698e4f8e8 to your computer and use it in GitHub Desktop.
Save lclarkmichalek/bb7898ad075625e0657b7b4698e4f8e8 to your computer and use it in GitHub Desktop.
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