I'm a little new to golang and trying to process some websocket data.
I get data in the form of:
type Event struct {
Event string `json:"event"`
Data interface{} `json:"data"`
}
And the Data
field is a json object that I want to process and get into the following struct:
type OrderBook struct {
Pair string `json:"pair"`
Timestamp string `json:"timestamp"`
Broker string `json:"broker"`
Bids []OrderBookItem `json:"bids"`
Asks []OrderBookItem `json:"asks"`
}
type OrderBookItem struct {
Price float64
Amount float64
}
from the websocket function:
....
case "data":
publish(e.Data) <--- this is where the error occurs
....
which calls:
func publish(data OrderBookResult) {
log.Println("Publishing trade data...", data)
o := &OrderBook{}
o.Pair = "BTCEUR"
o.Timestamp = data.Timestamp
o.Broker = "Bitstamp"
o.Asks = data.Asks
o.Bids = data.Bids
}
The error in the websocket function I get is the following:
cannot use e.Data (type interface {}) as type OrderBookResult in argument to publish: need type assertion
How can I "cast" the websocket struct into the new struct and reference fields, that are not defined on the websocket struct. I have a node.js
background and I haven't gotten my head around the strictness of go yet.
Thx