有人可以幫助我理解這些代碼嗎?在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{}”,這是什么意思,有官方文檔支持嗎?
1 回答

慕神8447489
TA貢獻1780條經驗 獲得超1個贊
在golang中,如果定義了一個變量但不使用它,那么就會報錯。通過使用_
名稱,您可以克服這個問題。我想每個人都已經_, err := doSomething()
在 golang 中看到過。var _ Store = &cache{}
與此沒有什么不同。這里最棒的是Store
一個接口,所以通過var _ Store = &cache{}
這樣做,它強制caches
實現接口Store
。如果caches
不實現該接口,您的代碼將無法編譯。這是多么厲害的伎倆呢?
- 1 回答
- 0 關注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消