登录
首页 >  Golang >  Go问答

ETCD集群中检索密钥的API

来源:stackoverflow

时间:2024-03-14 21:21:32 177浏览 收藏

今天golang学习网给大家带来了《ETCD集群中检索密钥的API》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

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

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

但是即使key不在集群中,错误也总是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 属性,如果您想检查您的响应是否包含密钥,您只需检查 count > 0len(res.kvs) > 0

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>