As it is mentioned in the RabbitMQ docs that tcp connections are expensive to make. So, for that concept of channel was introduced. Now i came across this example. In the
Of course, you shouldn't create a connection for each request. Make it a global variable or better part of an application context which you initialize once at startup.
You can handle connection errors by registering a channel using Connection.NotifyClose
:
func initialize() {
c := make(chan *amqp.Error)
go func() {
err := <-c
log.Println("reconnect: " + err.Error())
initialize()
}()
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
panic("cannot connect")
}
conn.NotifyClose(c)
// create topology
}