Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Build artifacts
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go workspace file
go.work

# Example binaries
/example/publisher/publisher
/example/consumer/consumer

# Temporary files
/tmp/
119 changes: 118 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,119 @@
# go-rmq
RabbitMQ Driver

A simple and easy-to-use Go RabbitMQ client library built on top of the official AMQP library.

## Features

- Simple connection management
- Easy message publishing
- Message consuming with custom handlers
- Automatic queue declaration
- Persistent message delivery
- Proper error handling and logging

## Installation

```bash
go get github.com/idprm/go-rmq
```

## Quick Start

### Publishing Messages

```go
package main

import (
"context"
"log"

"github.com/idprm/go-rmq"
)

func main() {
// Create client
client, err := rmq.NewClient(rmq.Config{
URL: "amqp://guest:guest@localhost:5672/",
})
if err != nil {
log.Fatal(err)
}
defer client.Close()

// Publish message
ctx := context.Background()
err = client.PublishMessage(ctx, "my_queue", []byte("Hello, RabbitMQ!"))
if err != nil {
log.Fatal(err)
}
}
```

### Consuming Messages

```go
package main

import (
"fmt"
"log"

"github.com/idprm/go-rmq"
)

func main() {
// Create client
client, err := rmq.NewClient(rmq.Config{
URL: "amqp://guest:guest@localhost:5672/",
})
if err != nil {
log.Fatal(err)
}
defer client.Close()

// Define message handler
handler := func(message []byte) error {
fmt.Printf("Received: %s\n", string(message))
return nil
}

// Start consuming (blocking)
err = client.ConsumeMessages("my_queue", handler)
if err != nil {
log.Fatal(err)
}
}
```

## Configuration

The `Config` struct supports the following options:

- `URL`: RabbitMQ connection URL (default: "amqp://guest:guest@localhost:5672/")

## API Reference

### Client Methods

- `NewClient(config Config) (*Client, error)` - Create a new RabbitMQ client
- `Close() error` - Close the connection
- `DeclareQueue(name string) error` - Declare a queue
- `PublishMessage(ctx context.Context, queueName string, message []byte) error` - Publish a message
- `ConsumeMessages(queueName string, handler func([]byte) error) error` - Consume messages
- `IsConnected() bool` - Check connection status

## Examples

See the `example/` directory for complete working examples:
- `example/publisher/` - Message publisher example
- `example/consumer/` - Message consumer example

## Requirements

- Go 1.18+
- RabbitMQ server

## License

MIT License
35 changes: 35 additions & 0 deletions example/consumer/consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"

"github.com/idprm/go-rmq"
)

func main() {
// Create RabbitMQ client
client, err := rmq.NewClient(rmq.Config{
URL: "amqp://guest:guest@localhost:5672/",
})
if err != nil {
log.Fatalf("Failed to create RabbitMQ client: %v", err)
}
defer client.Close()

// Queue name
queueName := "test_queue"

// Message handler
handler := func(message []byte) error {
fmt.Printf("Received message: %s\n", string(message))
return nil
}

// Start consuming messages
fmt.Printf("Starting to consume messages from queue '%s'...\n", queueName)
err = client.ConsumeMessages(queueName, handler)
if err != nil {
log.Fatalf("Failed to consume messages: %v", err)
}
}
9 changes: 9 additions & 0 deletions example/consumer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module consumer

go 1.24.7

require github.com/idprm/go-rmq v0.0.0

require github.com/rabbitmq/amqp091-go v1.10.0 // indirect

replace github.com/idprm/go-rmq => ../../
4 changes: 4 additions & 0 deletions example/consumer/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
9 changes: 9 additions & 0 deletions example/publisher/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module publisher

go 1.24.7

require github.com/idprm/go-rmq v0.0.0

require github.com/rabbitmq/amqp091-go v1.10.0 // indirect

replace github.com/idprm/go-rmq => ../../
4 changes: 4 additions & 0 deletions example/publisher/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
41 changes: 41 additions & 0 deletions example/publisher/publisher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/idprm/go-rmq"
)

func main() {
// Create RabbitMQ client
client, err := rmq.NewClient(rmq.Config{
URL: "amqp://guest:guest@localhost:5672/",
})
if err != nil {
log.Fatalf("Failed to create RabbitMQ client: %v", err)
}
defer client.Close()

// Queue name
queueName := "test_queue"

// Publish messages
ctx := context.Background()
for i := 0; i < 5; i++ {
message := fmt.Sprintf("Hello RabbitMQ! Message #%d", i+1)

err := client.PublishMessage(ctx, queueName, []byte(message))
if err != nil {
log.Printf("Failed to publish message: %v", err)
continue
}

fmt.Printf("Published: %s\n", message)
time.Sleep(1 * time.Second)
}

fmt.Println("All messages published successfully!")
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/idprm/go-rmq

go 1.24.7

require github.com/rabbitmq/amqp091-go v1.10.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
Loading