package rabbitmq import ( "fmt" "git.umbach.dev/app-idea/rest-api/modules/config" log "github.com/sirupsen/logrus" "github.com/streadway/amqp" ) var Conn *amqp.Connection func getConnectionString() string { cfg := &config.Cfg.RabbitMq return fmt.Sprintf("amqp://%s:%s@%s/", cfg.Username, cfg.Password, cfg.Host) } func Init() { conn, err := amqp.Dial(getConnectionString()) if err != nil { log.Fatalln("Failed to connect to RabbitMQ", err) } Conn = conn ch, err := conn.Channel() if err != nil { log.Fatalln("Failed to open a channel", err) } defer ch.Close() log.Debug("RabbitMQ connected") q, err := ch.QueueDeclare( "hello", // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { log.Fatalln("Failed to declare a queue", err) } body := "Hello World!" err = ch.Publish( "", // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ ContentType: "text/plain", Body: []byte(body), }) if err != nil { log.Fatalln("Failed to publish a message", err) } }