gateio / gatews

Gate Websocket V4 SDK
89 stars 35 forks source link

json: cannot unmarshal number into Go struct field xxx of type string #12

Closed ioogle closed 3 years ago

ioogle commented 3 years ago

This is a common error and happens in many cases, for example:

m.wsService.SetCallBack(gatews.ChannelFutureBalance, func(msg *gatews.UpdateMsg) {
    var accounts []gatews.FuturesAccountBook

    if m.conf.Debug && msg.Event == "update" {
        log.Printf("ChannelFutureBalance: %s\n", string(msg.Result))
    }

    if msg.Error != nil {
        errorHandler(convertWsError(msg.Error, excommon.GlobalMarketTypeFutures, excommon.Unknown))
        return
    }

    if msg.Event != "update" {
        return
    }

    if err := json.Unmarshal(msg.Result, &accounts); err != nil {
        errorHandler(convertWsError(err, excommon.GlobalMarketTypeFutures, excommon.Unknown)) // ①
    }
        // ...

① will show the error: json: cannot unmarshal number into Go struct field FuturesAccountBook.balance of type string. This kind of bug also occurs in the case of GateWsFuturesTrade and GateWsFuturesOrder et al.

It could be a solution to refactor the structs in customized code, but it will be much better if your guys can fix this problem inside this package.

revilwang commented 3 years ago

It seems there are type mismatch. We'll look into this issue

ioogle commented 3 years ago

Thanks

ioogle commented 3 years ago

Btw, the FuturesPosition struct should also be added. ❤️