为了账号安全,请及时绑定邮箱和手机立即绑定

为什么 RabbitMQ 的 PublishWithContext 不适用于 Go?

为什么 RabbitMQ 的 PublishWithContext 不适用于 Go?

Go
红糖糍粑 2023-03-07 14:27:49
我正在通过在 GoLang 中构建一个小型应用程序来学习 RabbitMQ - 在此处遵循此示例: https: //www.rabbitmq.com/tutorials/tutorial-one-go.html。我的项目具有以下结构:project└───cmd│   └───api│   │   main.go│   └───internal│   │   rabbitmq.go在cmd/internal/rabbitmq.go我有以下代码 - (错误是错误的):import (    ...    amqp "github.com/rabbitmq/amqp091-go")func NewRabbitMQ() (*RabbitMQ, error) {    // Initialise connection to RabbitMQ    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")    // Initialise Channel    ch, err := conn.Channel()     // Initialise Queue    q, err := ch.QueueDeclare(        "hello", // name        false,   // durable        false,   // delete when unused        false,   // exclusive        false,   // no-wait        nil,     // arguments    )    // Set Context    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)    defer cancel()    body := "Hello World!"    err = ch.PublishWithContext( // errors here       ctx,    // context       "",     // exchange       q.Name, // routing key       false,  // mandatory       false,  // immediate       amqp.Publishing{          ContentType: "text/plain",          Body:        []byte(body),       })    return &RabbitMQ{}, nil}据我所知,根据文档,这是应该如何实现的,所以我不确定为什么会出错。我曾尝试使用谷歌搜索来寻找解决此问题的帮助,但无济于事。我正在使用 Go 1.19,也许这是一个问题?
查看完整描述

1 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

这是一个简单的修复!问题是我使用的 RabbitMQ 版本较旧,所以我只需要更改我的go.mod版本:

require github.com/rabbitmq/amqp091-go v1.1.0

到:

require github.com/rabbitmq/amqp091-go v1.4.0


查看完整回答
反对 回复 2023-03-07
  • 1 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信