How to access the values of a message struct?
jpjamipark opened this issue · comments
Hi,
I'm currently using msg, err := pcapScanner.NextMessage()
and I'm trying to access the values contained in the msg struct. When I try to access using msg.MessageType
, I'm get an error:
./hello.go:69:51: msg.MessageType undefined (type iextp.Message has no field or method MessageType)
I know it has something to do with the inheritance between Message and other Message structs; what's the proper way of using this?
Hi! You can use a type switch or type assertion to unwrap the iextp.Message
interface into one of the concrete protocol types, then you can access the values. For example:
// Type switch
switch msg := msg.(type) {
case *tops.QuoteUpdateMessage:
fmt.Printf("%v,%v,%v,%v,%v,%v\n",
msg.Timestamp, msg.Symbol, msg.BidPrice,
msg.BidSize, msg.AskPrice, msg.AskSize)
case *tops.TradeReportMessage:
fmt.Printf("%v,%v,%v,%v\n", msg.Timestamp, msg.Symbol, msg.Price, msg.Size)
}
// Type assertion
if msg, ok := msg.(*tops.QuoteUpdateMessage); ok {
fmt.Printf("Got a quote message: %v", msg.MessageType)
}
The different message types in the TOPS and DEEP feeds are are described in the IEX market data documentation, or you can refer to the GoDoc.
Closing this, feel free to reopen if you have additional questions.