Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe methods does not return channel #28

Open
Termina1 opened this issue Jun 9, 2014 · 3 comments
Open

Subscribe methods does not return channel #28

Termina1 opened this issue Jun 9, 2014 · 3 comments
Assignees

Comments

@Termina1
Copy link

Termina1 commented Jun 9, 2014

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?

@Termina1 Termina1 changed the title Subscribe methods returns channel Subscribe methods not returns channel Jun 9, 2014
@Termina1 Termina1 changed the title Subscribe methods not returns channel Subscribe methods does not return channel Jun 9, 2014
@xiam
Copy link
Contributor

xiam commented Jun 9, 2014

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?

@xiam xiam self-assigned this Jun 9, 2014
@Termina1
Copy link
Author

Termina1 commented Jun 9, 2014

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 [email protected]
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:
#28 (comment)

@xiam xiam modified the milestone: pending-review Dec 9, 2014
@Igosuki
Copy link

Igosuki commented Jan 19, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants