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

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

golang 無法為結構分配接口 {}

golang 無法為結構分配接口 {}

Go
婷婷同學_ 2022-11-08 16:34:35
專家您好,我正在使用此庫將 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
查看完整描述

1 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

您正在嘗試分配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


查看完整回答
反對 回復 2022-11-08
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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