我面臨著一個無法支持的問題,我必須從Ethereum智能合約呼叫中解碼資料(但這不是主題,因為這部分作業),但我將這些資料恢復到一個interface{}.
而當我想把這個空的介面轉換為我可以使用的結構時,我得到了這樣的訊息:panic: interface conversion: interface {} is struct { SrcToken common.Address "json: "srcToken""; DstToken common.Address "json: "dstToken""; SrcReceiver common. 地址 "json: "srcReceiver""; DstReceiver common.Address "json: "dstReceiver""; Amount *big.Int "json: "金額""; MinReturnAmount *big.Int "json: "minReturnAmount""; Flags *big.Int "json: "flags""; Permit []uint8 "json: "許可證"" }, not oneinch.OneInchSwapDataDesc
這個結構是 :
type OneInchSwapDataDesc struct {
SrcToken common.Address
DstToken common.Address
SrcReceiver common.Address
DstReceiver common.Address
金額 *big.Int
最小返還金額 *big.Int
旗幟 *big.Int
Permit []uint8。
}
當我尋找型別和資料值時,我得到了 :
。
fmt.Println("Type: ", reflect.TypeOf(data))
fmt.Printf("Data: % v
", data)
// Logs:
// Type: struct { SrcToken common.Address "json: "srcToken""; DstToken common.Address "json: "dstToken""; SrcReceiver common.Address "json: "srcReceiver""; DstReceiver common. 地址 "json: "dstReceiver""; Amount *big.Int "json: "金額""; MinReturnAmount *big.Int "json: "minReturnAmount""; Flags *big.Int "json: "flags""; Permit []uint8 "json: "許可"" }
// Data: {SrcToken:0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 DstToken:0xc2132D05D31c914a87C6611C10748AEb04B58e8F SrcReceiver: 0x11431a89893025D2a48dCA4EddC396f8C8117187 DstReceiver:0x2C2d6E5E16FE924cE31784cdAd27C01D1BC62873 Amount: 10000000 MinReturnAmount: 9949450 Flags: 4 Permit:[]}
我認為這個問題來自TypeOf中的 "json",但我沒有找到任何技巧來清除它(事實上,我真的不確定這個問題是否真的來自于此)。
如果有人有想法,你會拯救我的大腦,使其不至于在地獄中被燒毀。 謝謝。
uj5u.com熱心網友回復:
介面有一個型別為struct { SrcToken ...}的值。它是一個未命名的型別。型別斷言只有在型別相同的情況下才會起作用,或者被斷言的型別是一個介面,并且底層介面型別實作了該介面。你試圖轉換的型別不是一個介面,而是一個結構。所以你可以這樣做:
value:=data.(struct {
SrcToken common.Address `json: "srcToken"`
DstToken common.Address`json: "dstToken"`
SrcReceiver common.Address`json: "srcReceiver"`
DstReceiver common.Address `json: "dstReceiver"
Amount *big.Int `json: "金額"`
MinReturnAmount *big.Int `json: "minReturnAmount"`/span>
Flags *big.Int `json: "flags"`/span>
Permit []uint8 `json: "permit"` })
一旦你有了這個,你就可以用value轉換為你想要的型別:
targetValue:=oneinch.OneInchSwapDataDesc(value)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329373.html
標籤:
