Skip to content

Instantly share code, notes, and snippets.

@literadix
Created August 20, 2018 05:09
Show Gist options
  • Save literadix/11d9bdaa77136103b7ff1e47e4eee98c to your computer and use it in GitHub Desktop.
Save literadix/11d9bdaa77136103b7ff1e47e4eee98c to your computer and use it in GitHub Desktop.
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