title: How to subscribe to multiple MQTT topics using the paho.mqtt.golang library?
body: <p>I try to connect to a MQTT broker running on an external server where I need to handle multiple different topics. The problem is, that <strong>only the last subscribed handler is called</strong>, even if the topic does not match the one passed as parameter to the <code>Subscribe()</code> method.</p>
  
  <p><strong>Example:</strong>
  sending any content to <code>p1/test</code> triggers the registrationHandler which only should listen for "<em>P1/controller/registration</em>". The other handlers or the DefaultPublishHandler are never called.</p>
  
  <p>A simplified version of my program:</p>
  
  <pre><code>func main(){
      var opts = MQTT.NewClientOptions()
      opts.AddBroker(address)
      opts.SetClientID("go-controller")
  
      opts.SetDefaultPublishHandler(func(client MQTT.Client, msg MQTT.Message) {
          log.Printf("topic: %s\n", msg.Topic())
      })
  
      client = MQTT.NewClient(opts)
      if token := client.Connect(); token.Wait() &amp;&amp; token.Error() != nil {
          log.Panicln(token.Error())
      }
  
      client.Subscribe("P1/#", 0, logHandler)
      client.Subscribe("P1/controller/registration", 0, registrationHandler)
  }
  
  var logHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
      ...
      log.Printf("Topic %s logged...\n", msg.Topic())
  }
  
  var registrationHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
      ...
      log.Printf("Topic %s registered...\n", message.Value)
  }
  </code></pre>
  
  <p><strong>EDIT #1</strong></p>
  
  <p>I found out that the oder of subscribing to topics matters. When I swap both subscription calls the correct handlers get called.</p>