我正在通過(guò)在 GoLang 中構(gòu)建一個(gè)小型應(yīng)用程序來(lái)學(xué)習(xí) RabbitMQ - 在此處遵循此示例: https: //www.rabbitmq.com/tutorials/tutorial-one-go.html。我的項(xiàng)目具有以下結(jié)構(gòu):project└───cmd│ └───api│ │ main.go│ └───internal│ │ rabbitmq.go在cmd/internal/rabbitmq.go我有以下代碼 - (錯(cuò)誤是錯(cuò)誤的):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}據(jù)我所知,根據(jù)文檔,這是應(yīng)該如何實(shí)現(xiàn)的,所以我不確定為什么會(huì)出錯(cuò)。我曾嘗試使用谷歌搜索來(lái)尋找解決此問(wèn)題的幫助,但無(wú)濟(jì)于事。我正在使用 Go 1.19,也許這是一個(gè)問(wèn)題?
1 回答

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
這是一個(gè)簡(jiǎn)單的修復(fù)!問(wèn)題是我使用的 RabbitMQ 版本較舊,所以我只需要更改我的go.mod
版本:
require github.com/rabbitmq/amqp091-go v1.1.0
到:
require github.com/rabbitmq/amqp091-go v1.4.0
- 1 回答
- 0 關(guān)注
- 165 瀏覽
添加回答
舉報(bào)
0/150
提交
取消