tencent cloud


SDK for Go

Last updated: 2022-07-22 16:43:21


    This document describes how to use open-source SDK to send and receive messages by using the SDK for Go as an example and helps you better understand the message sending and receiving processes.



    1. Run the following command to install the required package in the client environment:
      go get "github.com/rabbitmq/amqp091-go"
    1. After the installation is completed, import the package to your Go project file.
      import (amqp "github.com/rabbitmq/amqp091-go")

    After the import, you can use the client in your project.


    1. Establish the connection and communication channel.
      // Required parameters
      const (
      host     = "amqp-xx.rabbitmq.x.tencenttdmq.com" // Service access address
      username = "roleName" // Role name in the console
      password = "eyJrZX..." // Role key
      vhost    = "amqp-xx|Vhost" // Full name of the vhost to be used
      // Create a connection
      conn, err := amqp.Dial("amqp://" + username + ":" + password + "@" + host + ":5672/" + vhost)
      failOnError(err, "Failed to connect to RabbitMQ")
      defer func(conn *amqp.Connection) {
      err := conn.Close()
      if err != nil {
      // Establish a channel
      ch, err := conn.Channel()
      failOnError(err, "Failed to open a channel")
      defer func(ch *amqp.Channel) {
      err := ch.Close()
      if err != nil {
    Parameter Description
    host Cluster access address, which can be obtained from Access Address in the Operation column on the Cluster page. img
    username Role name, which can be copied on the Role Management page.
    password Role key, which can be copied in the Key column on the Role Management page.
    vhost Vhost name in the format of "cluster ID + | + vhost name", which can be copied on the Vhost page in the console. img
    1. Declare the exchange.
      // Declare the exchange (the name and type must be the same as those of the existing exchange)
      err = ch.ExchangeDeclare(
       "logs-exchange",   // Exchange name
       "fanout", // Exchange type
       true,     // durable
       false,    // auto-deleted
       false,    // internal
       false,    // no-wait
       nil,      // arguments
      failOnError(err, "Failed to declare a exchange")
    1. Publish messages.

      Messages can be directly sent to the exchange or the specified queue (hello world and work).
      Publish messages to the exchange:

      // Message content
      body := "this is new message."
      // Publish messages to the exchange   
      err = ch.Publish(
      "logs-exchange",         // exchange
      "", // Routing key (set whether the routing key is required based on the used exchange type). If exchange is not selected, this parameter will be the message queue name
      false,      // mandatory
      false,      // immediate
       ContentType: "text/plain",
       Body:        []byte(body),
      failOnError(err, "Failed to publish a message")

      Publish messages to the specified queue:

      // Publish messages to the specified message queue
      err = ch.Publish(
      "",         // exchange
      queue.Name, // routing key
      false,      // mandatory
      false,      // immediate
       ContentType: "text/plain",
       Body:        []byte(body),
      failOnError(err, "Failed to publish a message")
    1. Subscribe to messages.
      // Create a consumer and consume messages in the specified message queue
      msgs, err := ch.Consume(
      "message-queue", // message-queue
      "",           // consumer
      false,        // Set to manual acknowledgment as needed
      false,        // exclusive
      false,        // no-local
      false,        // no-wait
      nil,          // args
      failOnError(err, "Failed to register a consumer")
      // Get messages in the message queue
      forever := make(chan bool)
      go func() {
      for d := range msgs {
       log.Printf("Received a message: %s", d.Body)
       t := time.Duration(1)
       time.Sleep(t * time.Second)
       // Manually return the acknowledgment
      log.Printf(" [Consumer] Waiting for messages.")
    1. The consumer uses the routing key.
      // You need to specify the exchange and routing key in the message queue
      err = ch.QueueBind(
      q.Name, // queue name
      "routing_key",     // routing key
      "topic_demo", // exchange
      failOnError(err, "Failed to bind a queue")

    For detailed samples, see Demo or RabbitMQ Tutorials.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support