go操作rabbitmq发送接收消息代码

代码语言:golang

所属分类:其他

代码描述:go操作rabbitmq发送接收消息代码

代码标签: 发送 接收 消息

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

package main
 
import (
   "log"
   "github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
   if err != nil {
         log.Fatalf("%s: %s", msg, err)
    }
}
//发送消息
func send(){
     conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
     failOnError(err, "Failed to connect to RabbitMQ")
     defer conn.Close()
 
     ch, err := conn.Channel()
     failOnError(err, "Failed to open a channel")
     defer ch.Close()
 
     q, err := ch.QueueDeclare(
         "hello", // name
         false,   // durable
         false,   // delete when unused
         false,   // exclusive
         false,   // no-wait
         nil,     // arguments
     )
     failOnError(err, "Failed to declare a queue")
 
     body := "Hello World!"
     err = ch.Publish(
         "",     // exchange
         q.Name, // routing key
         false,  // mandatory
         false,  // immediate
        amqp.Publishing{
             ContentType: "text/plain",
             Body:        []byte(body),
         })
     log.Printf(" [x] Sent %s", body)
     failOnError(err, "Failed to publish a message")
}
func recv(){
    c.........完整代码请登录后点击上方下载按钮下载查看

网友评论0