gosexy / redis

Redis client for Go that maps the full redis command list into equivalent Go functions.
MIT License
167 stars 44 forks source link

Subscribe methods does not return channel #28

Open Termina1 opened 10 years ago

Termina1 commented 10 years ago

I wonder, why this methods (subscribe, psubscribe) do not return channels, but you should pass one as an argument? Isn't a creation of this channel a boilerplate code?

xiam commented 10 years ago

This is currently the way to use subscriptions with gosexy/redis:

rec := make(chan []string)

go func() {
    select {
    case ls = <-rec:
        t.Logf("Got: %v\n", ls)
    }
}()

go consumer.Subscribe(rec, "channel")

The problem I tried to solve was setting up the select before attempting to subscribe (subscribe is a blocking function) and that was the way that seemed right at the time. Obviously I'm no expert and I'm sure things could be better, what do you think of this? (untested).

var rec chan []string
var err error

if rec, err = consumer.SubscribeNonBlocking("channel"); err != nil {
    t.Logf("Failed: %q", err)
}

go func() {
    select {
    case ls = <-rec:
        t.Logf("Got: %v\n", ls)
    }
}()

Do you have any suggestions?

Termina1 commented 10 years ago

I just started learning Go and this question appeared, because I didn't see any point in passing this channel instead of returning new instance from subscribe function. But I'm very new to Go, so I don't know how things here should be done.

Best regards, Vyacheslav

On Mon, Jun 9, 2014 at 5:21 PM, Carlos Nieto notifications@github.com wrote:

This is currently the way to use subscriptions with gosexy/redis:

rec := make(chan []string)
go func() {
    select {
    case ls = <-rec:
        t.Logf("Got: %v\n", ls)
    }
}()
go consumer.Subscribe(rec, "channel")

The problem I tried to solve was setting up the select before attempting to subscribe (subscribe is a blocking function) and that was the way that seemed right at the time. Obviously I'm no expert and I'm sure things could be better, what do you think of this? (untested).

var rec chan []string
var err error
if rec, err = consumer.SubscribeNonBlocking("channel"); err != nil {
    t.Logf("Failed: %q", err)
}
go func() {
    select {
    case ls = <-rec:
        t.Logf("Got: %v\n", ls)
    }
}()

Do you have any suggestions?

Reply to this email directly or view it on GitHub: https://github.com/gosexy/redis/issues/28#issuecomment-45502738

Igosuki commented 9 years ago

@Termina1 Here is a simple use case : you might want to use that channel for multiple subscribers.