rabbitmq/publisher.go

33 lines
575 B
Go
Raw Normal View History

2021-08-09 03:45:44 +00:00
package rabbitmq
import (
"encoding/json"
"github.com/streadway/amqp"
)
// Publish new message to channel
func Publish(queueName string, data interface{}) error {
byteData, _ := json.Marshal(data)
// Set publishing data
message := generateData(byteData)
// Send
return channel.Publish(
"", // exchange
queueName, // routing key
false, // mandatory
false, // immediate
message,
)
}
// generateData ...
func generateData(body []byte) amqp.Publishing {
return amqp.Publishing{
ContentType: "application/json",
Body: body,
}
}