Giter VIP home page Giter VIP logo

go-rabbit's Introduction

Rabbitmq Package for Golang

It includes basic operations for Rabbitmq. These operations are:

  • Basic Publish/Consumer
  • Publish/Consumer RPC

Installation

go get github.com/root27/go-rabbit

Usage

Some usage examples are given below.

Basic Publish/Consumer

  • Basic Publish
package main

import (
	"encoding/json"
	"fmt"
	"log"

	amqplib "github.com/root27/go-rabbit"
)

func main() {

	// Connect to RabbitMQ
	conn, err := amqplib.Connect("amqp://guest:guest@localhost:5672/")
	if err != nil {
		fmt.Println(err)
		return
	}

	defer conn.Close()

	log.Printf("connected to RabbitMQ")

	// Send a message
	msg := SendMessage{"message": "Hello World!"}

	byteData, err := json.Marshal(msg)

	if err != nil {
		fmt.Println(err)
		return
	}

	_, err = amqplib.Send(conn, "test2", []byte(byteData), "application/json")
	if err != nil {
		fmt.Println(err)
		return
	}

	log.Printf("message sent: %s", msg)

}

type SendMessage map[string]interface{}
  • Basic Consumer
package main

import (
	"log"

	amqplib "github.com/root27/go-rabbit"
)

func main() {

	// Connect to RabbitMQ
	conn, err := amqplib.Connect("amqp://guest:guest@localhost:5672/")
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("connected to RabbitMQ")

	log.Printf("Messages waiting...")

	// Receive a message

	msgs, err := amqplib.Receive(conn, "test2")  // return a channel

	if err != nil {
		log.Fatal(err)
	}


    // msgs is a channel, so we can use it in a for loop


	for d := range msgs {

		log.Printf("received a message: %s", d)

		log.Printf("Type of data received: %T", d)
	}

}

Publish/Consumer RPC

  • Publish RPC
package main

import (
	"log"
	"strconv"

	amqplib "github.com/root27/go-rabbit"
)

func main() {

	conn, err := amqplib.Connect("amqp://guest:guest@localhost:5672/")

	if err != nil {
		panic(err)
	}

	testData := strconv.Itoa(5)


    // Send RPC accepts 4 parameters: connection, queue name, data, data type

    // Returns consume RPC response

	response, err := amqplib.Send_Rpc(conn, "test_rpc", []byte(testData), "text/plain")

	if err != nil {
		panic(err)
	}

	log.Printf("Response: %s", response)

}
  • Consumer RPC
package main

import (
	"log"
	"strconv"

	amqplib "github.com/root27/go-rabbit"
)

func main() {

	conn, err := amqplib.Connect("amqp://guest:guest@localhost:5672/")

	if err != nil {
		panic(err)
	}

	log.Printf("Connected to RabbitMQ")

	log.Printf("Waiting for RPC requests...")

    // Receive RPC accepts 4 parameters: connection, queue name, function, data type


    // Function must be a function that accepts a byte array and returns a byte array


	amqplib.Receive_Rpc(conn, "test_rpc", testFunction, "text/plain")

}

func testFunction(data []byte) []byte {

	integer, err := strconv.Atoi(string(data))

	if err != nil {
		panic(err)
	}

	return []byte(strconv.Itoa(integer * 2))

}

go-rabbit's People

Contributors

root27 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.