Is it possible to pass a method on a struct as a callback function in golang ?
E.g. Register a message handler :
func (mc MQTTClient) MessageHandler(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("TOPIC: %s
", msg.Topic())
fmt.Printf("MSG: %s
", msg.Payload())
}
func (mc MQTTClient) AddMessageHandler(){
//..
//subscribe to the topic /go-mqtt/sample and request messages to be delivered
//at a maximum qos of zero, wait for the receipt to confirm the subscription
if token := c.Subscribe("go-mqtt/sample", 0, mc.MessageHandler); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
}
Thank you very much !