Get value of struct with interfaces

前端 未结 2 1947
夕颜
夕颜 2021-01-29 10:39

I\'m trying to parse this petition (https://www.binance.com/api/v1/depth?symbol=MDABTC&limit=500)

I was having tons of problems to create an struct for it, so I used

2条回答
  •  甜味超标
    2021-01-29 11:19

    In Golang Spec

    For an expression x of interface type and a type T, the primary expression

    x.(T)
    

    asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

    Fetching an underlying value of string type you need to type assert to string from interface.

    books.Asks[0][0].(string)
    

    For performing an arithmetic operation on same you needs to convert string into float64 to take into account decimal values

    v := strconv.ParseFloat(books.Asks[0][0].(string), 64) * strconv.ParseFloat(books.Asks[0][1].(string), 64)
    

    Checkout code on Go playground

提交回复
热议问题