From 339940b810a90418d8d836f10cef85fc824255b3 Mon Sep 17 00:00:00 2001 From: kanade Date: Wed, 15 Jun 2022 14:39:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rabbitmq.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/rabbitmq.go b/rabbitmq.go index 9a1de46..922866d 100644 --- a/rabbitmq.go +++ b/rabbitmq.go @@ -3,22 +3,17 @@ package rabbitmq import ( "fmt" "github.com/streadway/amqp" - "log" ) var Rabbitmq = &rabbitmq{} type rabbitmq struct { - conn *amqp.Connection + Conn *amqp.Connection } // InitRabbitmq @Title 初始化rabbitmq -func InitRabbitmq(host string, port int, user, password string) { - var err error - Rabbitmq.conn, err = amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", user, password, host, port)) - if err != nil { - log.Fatal("rabbitmq链接失败,err:", err) - } +func InitRabbitmq(host string, port int, user, password string) (err error) { + Rabbitmq.Conn, err = amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", user, password, host, port)) return } @@ -27,7 +22,7 @@ func InitRabbitmq(host string, port int, user, password string) { // @Param msg string true "发送消息内容" // @Param delay int64 false "延时发送秒数" func (r *rabbitmq) Publish(exchangeName, msg string, delay ...int64) error { - ch, err := r.conn.Channel() + ch, err := r.Conn.Channel() if err != nil { return err } @@ -53,7 +48,7 @@ func (r *rabbitmq) Publish(exchangeName, msg string, delay ...int64) error { // @Param autoAck bool true "是否自动确认" // @Param callback func(amqp.Delivery)) true "回调处理" func (r *rabbitmq) Consume(queueName string, autoAck bool, callback func(amqp.Delivery)) error { - ch, err := r.conn.Channel() + ch, err := r.Conn.Channel() if err != nil { return err } @@ -83,5 +78,5 @@ func (r *rabbitmq) Consume(queueName string, autoAck bool, callback func(amqp.De // Close @Title 关闭资源 func (r *rabbitmq) Close() { - defer Rabbitmq.conn.Close() + defer Rabbitmq.Conn.Close() }