Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"log"
"time"
"github.com/gomodule/redigo/redis"
)
func main() {
//https://godoc.org/github.com/gomodule/redigo/redis#Pool
fmt.Println("Hello World")
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Println(err)
} else {
log.Println("Everything is fine!!!")
}
// defer c.Close()
/*
Commands:
redis-cli
PUBLISH example test
SUBSCRIBE example
*/
/// This is for Publisher
c.Do("PUBLISH", "example", "hello "+time.Now().String())
/// End here
/// This code is for Subscriber
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("example")
for {
switch v := psc.Receive().(type) {
case redis.Message:
fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
fmt.Println(v)
}
}
/// End here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment