title: Compare-and-swap in etcd v3?
body: <p>I'm trying to do a compare-and-store operation on a given key using etcd's Go client for the <code>v3</code> API. Seeing that <code>--swap-with-value</code> seems to be gone from <code>etcdctl put</code>, I suspect there's no corresponding method or argument in the client library either. Thus, I'm attempting my own implementation:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>However, Go refuses to compile with this message:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>What's wrong with this approach? Why does Go refuse to compile this snippet?</p>
  
  <p><strong>Edit:</strong> Thanks to JimB I found out that <code>etcdv3</code> is importing the old <code>golang/x/net/context</code> packages. I switched the <code>import</code> statements, but I still get something similar:</p>
  
  <pre><code>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)
  </code></pre>