gpt4 book ai didi

go - 如何为对象创建结构并在 golang 中获取值?

转载 作者:数据小太阳 更新时间:2023-10-29 03:44:16 24 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个函数,它将采用编码字符串,向 jsonrpc 服务发送请求,然后我需要选择一个无聊的数据(ask:assets:interface)并创建一个新变量

如何为对象创建结构:

{
"ask": {
"amount": 0,
"assets": [
{
"assetref": "74-266-27408",
"name": "USD",
"qty": 5000
}
]
},
"cancomplete": true,
"candisable": true,
"complete": false,
"offer": {
"amount": 0,
"assets": [
{
"assetref": "73-266-61482",
"name": "BTC",
"qty": 1
}
]
},
"requiredfee": 0
}

并制作一个解码十六进制字符串的函数:

type Order struct {
Ask []Ask `json:"ask"`
Cancomplete bool `json:"cancomplete"`
Candisable bool `json:"candisable"`
Complete bool `json:"complete"`
Offer []Offer `json:"offer"`
Requiredfee int `json:"requiredfee"`
}

func DecodeOrder(datahex string) (Order) {
order, _ := rpcClient.Call("decoderawexchange", datahex)
var d = make([]Order, 0, 100)
err := order.GetObject(&d)
if err != nil || d == nil {
panic(err)
}

return d
}

使用生成的结构解决的问题:

type Order struct {
Ask struct {
Amount float64 `json:"amount"`
Assets []struct {
Assetref string `json:"assetref"`
Name string `json:"name"`
Qty float64 `json:"qty"`
} `json:"assets"`
} `json:"ask"`
Cancomplete bool `json:"cancomplete"`
Candisable bool `json:"candisable"`
Complete bool `json:"complete"`
Offer struct {
Amount float64 `json:"amount"`
Assets []struct {
Assetref string `json:"assetref"`
Name string `json:"name"`
Qty float64 `json:"qty"`
} `json:"assets"`
} `json:"offer"`
Requiredfee float64 `json:"requiredfee"`
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com