Categorygithub.com/cloudresty/go-rabbitmq
modulepackage
0.0.8
Repository: https://github.com/cloudresty/go-rabbitmq.git
Documentation: pkg.go.dev

# README

Go Rabbitmq Package

go-rabbitmq is a very simple Go package for RabbitMQ.

What you can find on this page:

 

Go Rabbitmq Setup

 

Package version

You can check for the latest available version of go-rabbitmq package by visiting the tags page through your web browser or through the CLI like shown in the example below.

go list -m -versions github.com/cloudresty/go-rabbitmq

 

Package install

The package can be installed via go get command.

go get github.com/cloudresty/[email protected]

 

Package import

Like any other Go package this can be easily imported like shown in this example.

package main

import rabbitmq github.com/cloudresty/go-rabbitmq

// ...

 

Examples

Below you'll find two basic examples that demonstrates how to use go-rabbitmq package, both examples covering the basic functionality for a Publisher and also for a Subscriber.

 

Publisher example

package main

import (
    "encoding/json"
    "log"

    rabbitmq "github.com/cloudresty/go-rabbitmq"
    uuid "github.com/satori/go.uuid"
)

// Message structure
type Message struct {
    Name string
    Body string
    Time int64
}

// Main function
func main() {

    // Message content
    message := Message{"Cloudresty", "Hello", 1294706395881547000}

    // JSON Encode
    jsonMessage, err := json.Marshal(message)
    if err != nil {
        log.Println(err.Error())
    }

    // Publish message
    err = publishMessage("cloudresty", jsonMessage)
    if err != nil {
        log.Println(err.Error())
    }

}

// Publish a message to RabbitMQ
func publishMessage(exchange string, message []byte) error {

    // Message ID
    messageId := uuid.NewV4().String()

    // Create a new Publisher
    publisher := rabbitmq.NewPublisher(rabbitmq.ConnectionSettings{
        Host:     "localhost",
        Port:     "5672",
        User:     "guest",
        Password: "guest",
        Vhost:    "/",
    })

    // Publish a message
    err := publisher.Publish(rabbitmq.PublisherSettings{
        Exchange: rabbitmq.ExchangeSettings{
            Name:       exchange,
            Type:       "direct",
            Durable:    true,
            AutoDelete: false,
            Internal:   false,
            NoWait:     false,
        },
    }, rabbitmq.MessageSettings{
        MessageId:   messageId,
        ContentType: "text/plain",
        Body:        message,
    })
    if err != nil {
        return err
    }

    return nil

}

 

Consumer example

package main

import (
    log

    rabbitmq "github.com/cloudresty/go-rabbitmq"
)

// Main function
func main(){

    err := rabbitMQSubscribe("cloudresty", "cloudresty")
    if err != nil {
        log.Println(err.Error())
    }

}

// RabbitMQ Subscribe function
func rabbitMQSubscribe(exchange, queue string) error {

    // Create a new Subscriber
    subscriber := rabbitmq.NewSubscriber(rabbitmq.ConnectionSettings{
        Host:     "localhost",
        Port:     "5672",
        User:     "guest",
        Password: "guest",
        Vhost:    "/",
    })

    err := subscriber.Subscribe(rabbitmq.SubscriberSettings{
        Exchange: rabbitmq.ExchangeSettings{
            Name:       exchange,
            Type:       "direct",
            Durable:    true,
            AutoDelete: false,
            Internal:   false,
            NoWait:     false,
        },
        Queue: rabbitmq.QueueSettings{
            Name:       queue,
            RoutingKey: queue,
            Durable:    true,
            AutoDelete: false,
            Exclusive:  false,
            NoWait:     false,
        },
        RoutingKey: queue,
        NoWait:     false,
    }, func(message rabbitmq.MessageSettings) {

        // Handle message
        log.Println("Message ID: "+message.MessageId)
        log.Println("Message Body: "+string(message.Body))
    },
    "my-consumer")

    if err != nil {
        return err
    }

}

 


Copyright © Cloudresty

# Functions

NewConsumer creates a new Consumer.
NewPublisher creates a new Publisher.

# Structs

Struct that defines the RabbitMQ AMQP binding settings.
Struct that defines the RabbitMQ Channel settings.
Struct that defines the RabbitMQ AMQP connection settings.
No description provided by the author
Struct that defines the RabbitMQ AMQP consumer settings.
Struct that defines the RabbitMQ AMQP exchange settings.
Struct that defines the RabbitMQ AMQP message settings.
Publisher connects to RabbitMQ and publishes messages to a queue using the AMQP protocol.
Struct that defines the RabbitMQ AMQP publisher settings.
QoSSettings defines the RabbitMQ AMQP QoS settings.
Struct that defines the RabbitMQ AMQP queue settings.