客户访问中的golang语法

时间:2019-06-14 06:39:51

标签: go client-go

有人可以帮助我理解这些代码吗?

在客户开发项目中,有些代码我无法理解。 代码路径为\ 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{}

最后一行“ var _ St​​ore =&cache {}”是什么意思,是否有任何官方文件来支持它?

1 个答案:

答案 0 :(得分:2)

在golang中,如果定义变量并且不使用它,那么它将给出错误。通过使用_作为名称,您可以克服这一点。我认为每个人都已经在golang中看到_, err := doSomething()var _ Store = &cache{}与此不同。这里最棒的事情是Store是一个接口,因此通过执行var _ Store = &cache{},它强制执行caches来实现接口Store。如果caches没有实现该接口,则您的代码将无法编译。这真是太棒了吗?