gpt4 book ai didi

go - 用于在 etcd 集群中查找 key 的 API

转载 作者:数据小太阳 更新时间:2023-10-29 03:09:06 26 4
gpt4 key购买 nike

我正在尝试编写一段代码,我需要在其中查找某个 key 是否存在于 etcd 中。我试过这个:

_, err = kapi.Get(context.Background(), key, nil)
if err != nil {
return err
} else { ...

但即使键不在集群中,错误也总是nil

知道我在这里做错了什么吗?或者是否有任何其他 API 调用?

最佳答案

如果你在这里使用go client 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 属性,如果您想检查您的响应是否包含键,您只需检查 计数 > 0len(res.Kvs) > 0

关于go - 用于在 etcd 集群中查找 key 的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55350494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com