亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

client-go 中的 golang 語法

client-go 中的 golang 語法

Go
四季花海 2023-06-26 15:36:58
有人可以幫助我理解這些代碼嗎?在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不實現該接口,您的代碼將無法編譯。這是多么厲害的伎倆呢?



查看完整回答
反對 回復 2023-06-26
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號