client-go 中的 golang 语法
golang syntax in client-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 _ Store = &cache{}",什么意思,有官方文档支持吗?
在golang中,如果定义了一个变量而没有使用它,那么会报错。通过使用 _
作为名称,您可以解决这个问题。我想每个人都已经在 golang 中看到 _, err := doSomething()
了。 var _ Store = &cache{}
与此无异。这里最棒的是 Store
是一个接口,所以通过 var _ Store = &cache{}
这样做,它强制 caches
实现接口 Store
。如果 caches
没有实现该接口,您的代码将无法编译。这是多么棒的把戏?
有人可以帮助我理解这些代码吗?
在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 _ Store = &cache{}",什么意思,有官方文档支持吗?
在golang中,如果定义了一个变量而没有使用它,那么会报错。通过使用 _
作为名称,您可以解决这个问题。我想每个人都已经在 golang 中看到 _, err := doSomething()
了。 var _ Store = &cache{}
与此无异。这里最棒的是 Store
是一个接口,所以通过 var _ Store = &cache{}
这样做,它强制 caches
实现接口 Store
。如果 caches
没有实现该接口,您的代码将无法编译。这是多么棒的把戏?