Search code examples
gorabbitmqamqp

How to publish task by name to a RabbitMQ queue in Go?


I need to publish a task by name (creating a message on a queue with task name passed in headers, passing some args).

I've done this in Celery (Python) like this:

celery_app.send_task("task_name", args=("arg1", "arg2",))

Here's my code in Go to do the same thing:

headers := make(amqp.Table)
headers["argsrepr"] = []string{"arg1", "arg2"}
headers["task"] = "task_name"

body := ""
jsonBody, _ := json.Marshal(body)

err = ch.Publish(
  "",     // exchange
  "queue_name", // routing key
  false,  // mandatory
  false,  // immediate
  amqp.Publishing {
    DeliveryMode: amqp.Persistent,
    ContentType:  "application/json",
    Body:         jsonBody,
    Headers:      headers,
  })

But I get this error:

Failed to publish a message: table field "argsrepr" value [%!t(string=arg1) %!t(string=arg2)] not supported

I'm using "github.com/streadway/amqp" library to talk with my rabbitmq node. It seems that "send task by name" is not implemented in this library.


Solution

  • Here is an illustration of the symptoms. The fmt format string is apparently expecting boolean values.

    package main
    
    import "fmt"
    
    func main() {
        s := fmt.Sprintf("%t %t", "arg1", "arg2")
        fmt.Println(s)
        t := fmt.Sprintf("%t %t", true, false)
        fmt.Println(t)
    }
    

    Output:

    %!t(string=arg1) %!t(string=arg2)
    true false