專家您好,我正在使用此庫將 K/V 存儲在緩存中"github.com/bluele/gcache"我存儲的值就是這個數(shù)據(jù)結(jié)構type LatestBlockhashCacheResult struct { Blockhash string `json:"blockhash"` LastValidBlockHeight uint64 `json:"lastValidBlockHeight"` // Slot. CommitmentType string `json:"commitmentType"`}lbhr := LatestBlockhashCacheResult{ Blockhash: lbh.Value.Blockhash.String(), LastValidBlockHeight: lbh.Value.LastValidBlockHeight, CommitmentType: string(commitmentType), } gc.SetWithExpire(lbh.Value.LastValidBlockHeight, lbhr, time.Hour*10)我對檢索緩存沒有問題,但無法對其進行類型轉(zhuǎn)換c, _ := gc.Get(rf.LastValidBlockHeight) fmt.Printf("%T\n", c)所以當我嘗試這個var c = LatestBlockhashCacheResult{} c, _ = gc.Get(rf.LastValidBlockHeight)這引發(fā)了我的錯誤 cannot assign interface {} to c (type LatestBlockhashCacheResult) in multiple assignment: need type assertion
1 回答

MYYA
TA貢獻1868條經(jīng)驗 獲得超4個贊
您正在嘗試分配interface{}給類型化變量。為此,您需要首先嘗試將interface{}值轉(zhuǎn)換為特定類型
val, err := gc.Get(rf.LastValidBlockHeight)
if err != nil {
// handle
}
c, ok := val.(LatestBlockhashCacheResult)
if !ok {
// val has different type than LatestBlockhashCacheResult
}
參考:https:
//go.dev/tour/methods/15
https://go.dev/tour/methods/16
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消