doushenjia8514 2018-06-28 17:56
浏览 318


I currently have a Go code that can subscribe and print sensor data that is published to a certain topic. Here is my code:

package main

import (
    MQTT ""

func onMessageReceived(client MQTT.Client, message MQTT.Message) {
    //fmt.Printf("Received message on topic: %s
Message: %s
", message.Topic(), message.Payload())
", message.Payload())

func main() {
    //MQTT.DEBUG = log.New(os.Stdout, "", 0)
    //MQTT.ERROR = log.New(os.Stdout, "", 0)
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)

    hostname, _ := os.Hostname()

    server := flag.String("server", "tcp://", "The full url of the MQTT server to connect to ex: tcp://")
    topic := flag.String("topic", "topic/sensorTemperature", "Topic to subscribe to")

    qos := flag.Int("qos", 0, "The QoS to subscribe to messages at")
    clientid := flag.String("clientid", hostname+strconv.Itoa(time.Now().Second()), "A clientid for the connection")
    username := flag.String("username", "", "A username to authenticate to the MQTT server")
    password := flag.String("password", "", "Password to match username")

    connOpts := MQTT.NewClientOptions().AddBroker(*server).SetClientID(*clientid).SetCleanSession(true)
    if *username != "" {
        if *password != "" {
    tlsConfig := &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}

    connOpts.OnConnect = func(c MQTT.Client) {
        if token := c.Subscribe(*topic, byte(*qos), onMessageReceived); token.Wait() && token.Error() != nil {

    client := MQTT.NewClient(connOpts)
    if token := client.Connect(); token.Wait() && token.Error() != nil {
    } else {
        fmt.Printf("Connected to %s
", *server)


Instead of subscribing to messages like this, I want to put the part of the code that subscribes in a Goroutine. I want to be able to call go func onMessageReceived. How can I do that if this function is called in c.Subscribe? And how can I add a sync.WaitGroup parameter in? Thank you.

  • 写回答

1条回答 默认 最新

  • donglieshe4692 2018-06-28 18:49

    Since you're passing the function as a parameter to another function, you don't get to control the way in which it is called. However, you do have total control over what happens inside the function - which means you can start a goroutine there:

    func onMessageReceived(client MQTT.Client, message MQTT.Message) {
        go func() {
    ", message.Payload())

    So, onMessageReceived itself will still be called synchronously by MQTT, but it will just start a goroutine and immediately return. You could also define a separate function and call that with go instead of an anonymous function:

    func onMessageReceived(client MQTT.Client, message MQTT.Message) {
        go messageHandler(client, message)
    func messageHandler(client MQTT.Client, message MQTT.Message) {
    ", message.Payload())

    That's just a matter of how you want to organize your code. If it's a short handler I'd probably stick with the anonymous function (short enough that you can see the entire anonymous func on one screen); for a longer function I'd break it up or break it out into a named function.

    Since you can't pass in any extra parameters, if you want to use a WaitGroup, it will have to be global:

    var wg = new(sync.WaitGroup)
    func onMessageReceived(client MQTT.Client, message MQTT.Message) {
        go func() {
            defer wg.Done()
    ", message.Payload())
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序