1

I'm trying to do a compare-and-store operation on a given key using etcd's Go client for the v3 API. Seeing that --swap-with-value seems to be gone from etcdctl put, I suspect there's no corresponding method or argument in the client library either. Thus, I'm attempting my own implementation:

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

However, Go refuses to compile with this message:

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)

What's wrong with this approach? Why does Go refuse to compile this snippet?

Edit: Thanks to JimB I found out that etcdv3 is importing the old golang/x/net/context packages. I switched the import statements, but I still get something similar:

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)
Miguel
  • 3,466
  • 8
  • 38
  • 67
  • 2
    You're using the old `golang.org/x/net/context` package when you should be using the `context` package in the std library. – JimB Jul 31 '17 at 17:41
  • It's not me, in fact, but `etcd` :-( I tried switching to the old package and I got a similar error (please check my edit). – Miguel Jul 31 '17 at 18:09
  • 2
    That's because you're using a "method expression", which means that you're calling a method without the implied receiver. I'm not familiar with their client package, and I'm not sure why the client has a `KV` field at all, but the Txn example under [`KV`](https://godoc.org/github.com/coreos/etcd/clientv3#KV) at least shows you need to need to use `NewKv` to be able to call `Txn` – JimB Jul 31 '17 at 18:23
  • Thank you very much JimB. I wish I could approve your comment as an answer. It works now :-) – Miguel Jul 31 '17 at 18:28
  • no prob. I was going to try and look to see _what_ the KV field is to explain the behavior when I have a moment. If someone more familiar with that package know, they can certainly write up a complete answer first. – JimB Jul 31 '17 at 18:30

0 Answers0