2 回答
TA贡献2012条经验 获得超12个赞
假设您正在使用它,并且通过查看其来源,在我看来,结果的具体类型将是 ,如果是这种情况,那么您可以执行此操作。map[string]interface{}
res, err := api.Query("AssetPairs", map[string]string{})
if err != nil {
log.Fatal(err)
}
pairs, ok := res.(map[string]interface{})
if !ok {
log.Fatal("unsupported type")
}
for k, v := range pairs {
fmt.Printf("key=%s value=%+v\n ", k, v)
}
TA贡献1900条经验 获得超5个赞
正如前面的响应中提到的,我们看到返回的接口变成了map[string]interface{},下面的代码将执行检索类型的技巧:
for _, v := range d.(map[string]interface{}) {
switch v.(type) {
case map[string]interface{}:
fmt.Println("Its another map of string interface")
case interface{}:
fmt.Println("its an interface")
case string:
fmt.Println("its a string")
case []string:
fmt.Println("its a string array")
case float32:
fmt.Println("its a float32")
case float64:
fmt.Println("its a float64")
default:
fmt.Printf("Different thing, %T\n", v)
}
}
此处代码: https://play.golang.org/p/81LLYSvJVf8
但是,我建议使用显式类型,这将使您的生活更加轻松:
// Generated by https://quicktype.io
type KrakenTypes struct {
Error []interface{} `json:"error"`
Result map[string]Result `json:"result"`
}
type Result struct {
Altname string `json:"altname"`
Wsname *string `json:"wsname,omitempty"`
AclassBase Aclass `json:"aclass_base"`
Base string `json:"base"`
AclassQuote Aclass `json:"aclass_quote"`
Quote FeeVolumeCurrency `json:"quote"`
Lot Lot `json:"lot"`
PairDecimals int64 `json:"pair_decimals"`
LotDecimals int64 `json:"lot_decimals"`
LotMultiplier int64 `json:"lot_multiplier"`
LeverageBuy []int64 `json:"leverage_buy"`
LeverageSell []int64 `json:"leverage_sell"`
Fees [][]float64 `json:"fees"`
FeesMaker [][]float64 `json:"fees_maker"`
FeeVolumeCurrency FeeVolumeCurrency `json:"fee_volume_currency"`
MarginCall int64 `json:"margin_call"`
MarginStop int64 `json:"margin_stop"`
Ordermin *string `json:"ordermin,omitempty"`
}
在这里,我们可以在读取响应后使用json解码,因此可以避免迭代以找出每个级别的类型,并且我们可以直接访问每个成员。
此处的完整代码:https://play.golang.org/p/v3tlroyx1mW
- 2 回答
- 0 关注
- 78 浏览
添加回答
举报