Get value of struct with interfaces

2020-05-09 18:55发布

问题:

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 an automated tool, this is what my struct looks like:

type orderBook struct {
    Bids         [][]interface{} `json:"Bids"`
    Asks         [][]interface{} `json:"Asks"`
}

I recover and parse the petition by doing:

url := "https://www.binance.com/api/v1/depth?symbol=MDABTC&limit=500"
resp, err := http.Get(url)
if err != nil {
    panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    panic(err)
}else{
    book := orderBook{}
if err := json.Unmarshal(body, &book); err != nil {
    panic(err)
}

But whenever I try to make an operation with the struct, like:

 v := book.Asks[i][0] * book.Asks[i][1]

I get an error:

invalid operation: book.Asks[i][0] * book.Asks[i][1] (operator * not defined on interface)

How do I define it? Do I need to create an struct for bids/asks, if so, how would that look like?

Sorry if this seems basic, I just started learning go.

回答1:

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



回答2:

Note that you could define proper structs that unmarshal from the JSON document by implementing the json.Unmarshaler interface.

For example (on the Go Playground):

type OrderBook struct {
  Asks, Bids   []Order
  LastUpdateId int
}

type Order struct {
  Price, Volume float64
}

func (o *Order) UnmarshalJSON(bs []byte) error {
  values := make([]interface{}, 0, 3)
  err := json.Unmarshal(bs, &values)
  if err != nil {
    return err
  }
  // TODO: more error checking re: len(values), and their types.
  price, err := strconv.ParseFloat(values[0].(string), 10)
  if err != nil {
    return err
  }
  volume, err := strconv.ParseFloat(values[1].(string), 10)
  if err != nil {
    return err
  }
  *o = Order{price, volume}
  return nil
}

As such, unmarshaling those documents looks idiomatic:

func main() {
  book := OrderBook{}
  err := json.Unmarshal([]byte(jsonstr), &book)
  if err != nil {
    panic(err)
  }
  fmt.Printf("Asks:   %#v\n", book.Asks)
  fmt.Printf("Bids:   %#v\n", book.Bids)
  fmt.Printf("Update: %#v\n", book.LastUpdateId)
  // Asks:   []main.Order{main.Order{Price:0.00013186, Volume:167}, main.Order{Price:0.00013187, Volume:128}, ...
  // Bids:   []main.Order{main.Order{Price:0.00013181, Volume:110}, main.Order{Price:0.00013127, Volume:502}, ...
  // Update: 14069188
}