drnxnbf199049 2019-06-14 06:39 采纳率: 100%
浏览 16
已采纳

客户访问中的golang语法[重复]

This question already has an answer here:

someone can help me to understand these code?

in the client-go project, there are some codes i can't understand. code path is \tols\cache\store.go

    Add(obj interface{}) error
    Update(obj interface{}) error
    Delete(obj interface{}) error
    List() []interface{}
    ListKeys() []string
    Get(obj interface{}) (item interface{}, exists bool, err error)
    GetByKey(key string) (item interface{}, exists bool, err error)

    // Replace will delete the contents of the store, using instead the
    // given list. Store takes ownership of the list, you should not reference
    // it after calling this function.
    Replace([]interface{}, string) error
    Resync() error
}

type cache struct {
    // cacheStorage bears the burden of thread safety for the cache
    cacheStorage ThreadSafeStore
    // keyFunc is used to make the key for objects stored in and retrieved from items, and
    // should be deterministic.
    keyFunc KeyFunc
}

var _ Store = &cache{}

the last line "var _ Store = &cache{}", what is this mean, is there any officials document to support it?

</div>
  • 写回答

1条回答 默认 最新

  • doutan3463 2019-06-14 06:48
    关注

    In golang, if define a variable and do not use it, then it will give an error. By using _ as the name, you can overcome this. I think everybody already saw _, err := doSomething() in golang. var _ Store = &cache{} is not different from that. The awesome thing in here is Store is an interface, so by doing var _ Store = &cache{} this, it enforces caches to implement interface Store. If caches does not implement the interface, your code won't compile. How awesome trick is that?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?