我正在尝试编写一个代码,我需要在其中查找 etcd 中是否存在键。我试过这个:
_, err = kapi.Get(context.Background(), key, nil)
if err != nil {
return err
} else { ...
但即使密钥不在集群中,错误也始终是nil
。
知道我在这里做错了什么吗?或者是否有任何其他 API 调用要进行?
如果您在此处使用 go 客户端 v3 KV 客户端:https://godoc.org/go.etcd.io/etcd/clientv3#KV
它返回以下类型:https://godoc.org/go.etcd.io/etcd/etcdserver/etcdserverpb#RangeResponse
type RangeResponse struct {
Header *ResponseHeader `protobuf:"bytes,1,opt,name=header" json:"header,omitempty"`
// kvs is the list of key-value pairs matched by the range request.
// kvs is empty when count is requested.
Kvs []*mvccpb.KeyValue `protobuf:"bytes,2,rep,name=kvs" json:"kvs,omitempty"`
// more indicates if there are more keys to return in the requested range.
More bool `protobuf:"varint,3,opt,name=more,proto3" json:"more,omitempty"`
// count is set to the number of keys within the range when requested.
Count int64 `protobuf:"varint,4,opt,name=count,proto3" json:"count,omitempty"`
}
如您所见,有一个Count
属性和一个Kvs
属性,如果您想检查响应是否包含键,您可以检查该Count > 0
或len(res.Kvs) > 0