Created
August 20, 2018 05:09
-
-
Save literadix/11d9bdaa77136103b7ff1e47e4eee98c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/go-redis/redis" | |
"fmt" | |
"time" | |
) | |
func ExampleNewClient() { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "zot:6379", | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
pong, err := client.Ping().Result() | |
fmt.Println(pong, err) | |
// Output: PONG <nil> | |
pubsub := client.Subscribe("test") | |
_, err = pubsub.Receive() | |
if err != nil { | |
panic(err) | |
} | |
// Go channel which receives messages. | |
ch := pubsub.Channel() | |
time.AfterFunc(time.Second, func() { | |
// When pubsub is closed channel is closed too. | |
//_ = pubsub.Close() | |
}) | |
// Consume messages. | |
for { | |
msg, ok := <-ch | |
if !ok { | |
break | |
} | |
fmt.Println(msg.Channel, msg.Payload) | |
} | |
} | |
func main() { | |
// https://godoc.org/github.com/go-redis/redis#example-PubSub | |
ExampleNewClient() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment