我正在尝试使用 etcd 的v3
API 的 Go 客户端对给定的密钥进行比较和存储操作。看到--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)