Skip to content

Instantly share code, notes, and snippets.

@peterbourgon
Created June 26, 2013 18:20
Show Gist options
  • Save peterbourgon/5869939 to your computer and use it in GitHub Desktop.
Save peterbourgon/5869939 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/streadway/amqp"
"log"
)
func main() {
produce()
}
const (
queue_name = "asdfasdfasdf"
exchange_name = "dawfeasdfawefasd"
)
func produce() {
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Connection.open: %s", err)
}
log.Printf("Got connection %s \n", conn)
channel, err := conn.Channel()
if err != nil {
log.Fatalf("channel.open: %s", err)
}
log.Printf("Got channel %s \n", channel)
err = channel.ExchangeDeclare(exchange_name, "direct", true, false, false, false, nil)
if err != nil {
log.Fatalf("ExchangeDeclare: %s", err)
}
err = publish(channel, []byte("foooooooooobarararar"))
if err != nil {
log.Fatalf("Error publishing msg: %s", err)
}
}
func publish(channel *amqp.Channel, body []byte) error {
msg := amqp.Publishing{
Headers: amqp.Table{},
ContentType: "text/plain",
ContentEncoding: "",
Body: body,
DeliveryMode: amqp.Persistent,
Priority: 0,
}
err := channel.Publish(
exchange_name, // publish to default exchange
queue_name, // routing to 0 or more queues
false, // mandatory
false, // immediate
msg)
if err != nil {
return fmt.Errorf("Exchange Publish: %s", err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment