Golang RabbitMQ Client amqp-"Hello world!"

Source: Internet
Author: User
Tags rabbitmq

Sender Program
Package Mainimport ("FMT"    "Log"    "GITHUB.COM/STREADWAY/AMQP") Func failOnError (err error, MSGstring) {    ifErr! =Nil {log. Fatalf ("%s:%s", MSG, err) Panic (FMT. Sprintf ("%s:%s", MSG, err)) }}func Main () {conn, err:= Amqp. Dial ("Amqp://guest:[email protected]: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 usused        false,//Exclusive        false,//no-waitNil//arguments) FailOnError (err,"Failed to declare a queue") Body:="Hello"Err=Ch. Publish ("",//ExchangeQ.name,//Routing Key        false,//Mandatory        false,//ImmediateAMQP. publishing{ContentType:"Text/plain", Body: []byte(body),}) FailOnError (Err,"Failed to publish a message")}
Acceptance procedures
Package Mainimport ("FMT"    "Log"    "GITHUB.COM/STREADWAY/AMQP") Func failOnError (err error, MSGstring) {    ifErr! =Nil {log. Fatalf ("%s:%s", MSG, err) Panic (FMT. Sprintf ("%s:%s", MSG, err)) }}func Main () {conn, err:= Amqp. Dial ("Amqp://guest:[email protected]: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 usused        false,//Exclusive        false,//no-waitNil//arguments) FailOnError (err,"Failed to declare a queue") msgs, err:=Ch. Consume (Q.name,//Queue        "",//Consumer        true,//Auto-ack        false,//Exclusive        false,//no-local        false,//no-waitNil//args) FailOnError (err,"Failed to register a consumer") Forever:= Make(ChanBOOL) go func () { forD: =range msgs {log. Printf ("Received a message:%s", D.body)} } () log. Printf ("[*] waiting for messages. To exit Press CTRL + C")    <-Forever}

Golang RabbitMQ Client amqp-"Hello world!"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.