diff --git a/README.md b/README.md index 5553908..5381e80 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,31 @@ go get github.com/wagslane/go-rabbitmq ## 🚀 Quick Start Consumer +### Default options + ```go -consumer, err := rabbitmq.GetConsumer( +consumer, err := rabbitmq.NewConsumer("amqp://user:pass@localhost") +if err != nil { + log.Fatal(err) +} +err = consumer.StartConsuming( + func(d rabbitmq.Delivery) bool { + log.Printf("consumed: %v", string(d.Body)) + // true to ACK, false to NACK + return true + }, + "my_queue", + []string{"routing_key1", "routing_key2"} +) +if err != nil { + log.Fatal(err) +} +``` + +### With options + +```go +consumer, err := rabbitmq.NewConsumer( "amqp://user:pass@localhost", // can pass nothing for no logging func(opts *rabbitmq.ConsumerOptions) { @@ -56,16 +79,27 @@ err = consumer.StartConsuming( if err != nil { log.Fatal(err) } - -// block main thread so consumers run forever -forever := make(chan struct{}) -<-forever ``` ## 🚀 Quick Start Publisher +### Default options + +```go +publisher, returns, err := rabbitmq.NewPublisher("amqp://user:pass@localhost") +if err != nil { + log.Fatal(err) +} +err = publisher.Publish([]byte("hello, world"), []string{"routing_key"}) +if err != nil { + log.Fatal(err) +} +``` + +### With options + ```go -publisher, returns, err := rabbitmq.GetPublisher( +publisher, returns, err := rabbitmq.NewPublisher( "amqp://user:pass@localhost", // can pass nothing for no logging func(opts *rabbitmq.PublisherOptions) { diff --git a/consume.go b/consume.go index 9850b49..db5e48e 100644 --- a/consume.go +++ b/consume.go @@ -25,8 +25,8 @@ type Delivery struct { amqp.Delivery } -// GetConsumer returns a new Consumer connected to the given rabbitmq server -func GetConsumer(url string, optionFuncs ...func(*ConsumerOptions)) (Consumer, error) { +// NewConsumer returns a new Consumer connected to the given rabbitmq server +func NewConsumer(url string, optionFuncs ...func(*ConsumerOptions)) (Consumer, error) { options := &ConsumerOptions{} for _, optionFunc := range optionFuncs { optionFunc(options) diff --git a/examples/consumer/main.go b/examples/consumer/main.go index e4e3858..2e83a37 100644 --- a/examples/consumer/main.go +++ b/examples/consumer/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - consumer, err := rabbitmq.GetConsumer( + consumer, err := rabbitmq.NewConsumer( "amqp://user:pass@localhost", // can pass nothing for no logging func(opts *rabbitmq.ConsumerOptions) { diff --git a/examples/publisher/main.go b/examples/publisher/main.go index fe6c0d1..48a3ec3 100644 --- a/examples/publisher/main.go +++ b/examples/publisher/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - publisher, returns, err := rabbitmq.GetPublisher( + publisher, returns, err := rabbitmq.NewPublisher( "amqp://user:pass@localhost", // can pass nothing for no logging func(opts *rabbitmq.PublisherOptions) { diff --git a/publish.go b/publish.go index 7e0342b..88ed32c 100644 --- a/publish.go +++ b/publish.go @@ -60,12 +60,12 @@ type PublisherOptions struct { Logging bool } -// GetPublisher returns a new publisher with an open channel to the cluster. +// NewPublisher returns a new publisher with an open channel to the cluster. // If you plan to enforce mandatory or immediate publishing, those failures will be reported // on the channel of Returns that you should setup a listener on. // Flow controls are automatically handled as they are sent from the server, and publishing // will fail with an error when the server is requesting a slowdown -func GetPublisher(url string, optionFuncs ...func(*PublisherOptions)) (Publisher, <-chan Return, error) { +func NewPublisher(url string, optionFuncs ...func(*PublisherOptions)) (Publisher, <-chan Return, error) { options := &PublisherOptions{} for _, optionFunc := range optionFuncs { optionFunc(options)