dsqpx86002 2018-06-17 13:12
浏览 74
已采纳

如何在Go中按名称将任务发布到RabbitMQ队列?

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.

  • 写回答

1条回答 默认 最新

  • dongpang9573 2018-06-17 13:25
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable