How to implement Golang rabbitMQ producers and consumers
Today, the editor will share with you the relevant knowledge points about how to achieve Golang rabbitMQ producers and consumers. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Consumer package mainimport ("fmt"github.com/streadway/amqp") func failOnError (err error, msg string) {if err! = nil {fmt.Println ("% s:% s", msg, err)}} / / can only operate func main () {conn on the server where rabbitmq is installed. Err: = amqp.Dial ("amqp://mquser:123456@127.0.0.1/toutiao") 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 ("toutiao.web.test") / / queue name false, / / durable false, / / delete when unused false, / / exclusive false, / / no-wait nil, / / arguments) failOnError (err, "Failed to declare a queue") msgs, err: = ch.Consume (q.Name) / / queue "toutiao", / / consumer true, / / auto-ack,true disappear after consumption false, / / exclusive false, / / no-local false, / / no-wait nil, / / args) failOnError (err) "Failed to register a consumer") forever: = make (chan bool) go func () {for d: = range msgs {fmt.Println (fmt.Sprintf ("returned message:% s", d.Body))} () fmt.Println ("[*] Waiting for messages. To exit press CTRL+C ")