在etcd v3中进行比较和交换?

时间:2017-07-31 17:39:09

标签: go etcd etcd3

我正在尝试使用etcd的Go客户端对v3 API对给定密钥执行比较和存储操作。看到--swap-with-value似乎已从etcdctl put消失,我怀疑客户端库中没有相应的方法或参数。因此,我正在尝试自己的实现:

res, err := etcdv3.KV.Txn(context.Background()).If(
    etcdv3.Compare(etcdv3.ModRevision(c.path), "=", c.modRevision),
).Then(
    etcdv3.OpPut(c.path, string(data)),
    etcdv3.OpGet(c.path),
).Commit()

if err != nil {
    return err
}
if !res.Succeeded {
    return fmt.Errorf("A newer revision exists.")
}

// Return the current `ModRevision` on success.
return res.Responses[1].GetResponseRange().Kvs[0].ModRevision

但是,Go拒绝使用此消息进行编译:

main.go:55: not enough arguments in call to method expression clientv3.KV.Txn
    have ("context".Context)
    want (clientv3.KV, "github.com/miguel/myproject/vendor/golang.org/x/net/context".Context)

这种方法有什么问题?为什么Go拒绝编译这个片段?

修改:感谢JimB,我发现etcdv3正在导入旧的golang/x/net/context软件包。我切换了import语句,但我仍然得到类似的东西:

main.go:55: not enough arguments in call to method expression clientv3.KV.Txn
    have ("github.com/miguel/myproject/vendor/golang.org/x/net/context".Context)
    want (clientv3.KV, "github.com/miguel/myproject/vendor/golang.org/x/net/context".Context)

0 个答案:

没有答案