專家您好,我正在使用此庫將 K/V 存盤在快取中
"github.com/bluele/gcache"
我存盤的值就是這個資料結構
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)
我對檢索快取沒有問題,但無法對其進行型別轉換
c, _ := gc.Get(rf.LastValidBlockHeight)
fmt.Printf("%T\n", c)
所以當我嘗試這個
var c = LatestBlockhashCacheResult{}
c, _ = gc.Get(rf.LastValidBlockHeight)
這引發了我的錯誤
cannot assign interface {} to c (type LatestBlockhashCacheResult) in multiple assignment: need type assertion
uj5u.com熱心網友回復:
您正在嘗試分配interface{}給型別化變數。為此,您需要首先嘗試將interface{}值轉換為特定型別
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/429834.html
標籤:走
