Go 1.21 and etcd 3.5.9.
I have a go code that establishes communication with etcd db:
func main() {
endpoints := []string{"localhost:2379"}
certFile := "client.pem"
keyFile := "client.key"
caFile := "ca.pem"
cert, _ := tls.LoadX509KeyPair(certFile, keyFile)
caCert, _ := os.ReadFile(caFile)
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
config := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
client, _ := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
TLS: config,
})
defer client.Close()
...
}
Then I try to perform a transaction against etcd db:
...
cc, _ := context.WithTimeout(context.Background(), time.Second)
r, e := client.KV.Txn(cc).If(clientv3.Compare(clientv3.Value("hello"), "=", "world")).Then(op(0)).Else(op(1)).Commit()
fmt.Println(r, e)
}
func op(i int) clientv3.Op {
fmt.Printf("Custom logic executed within block %d\n", i)
g := fmt.Sprintf("hello%d", i)
return clientv3.OpGet(g)
}
My problem is that I got output to console:
Custom logic executed within block 0
Custom logic executed within block 1
But I only expect to see Custom logic executed within block 0, since I do have a key hello with a value world inside an etcd db. What am I doing wrong?