Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftntming/4f9dbf5cdb5edf0db5e74f4550bc4307 to your computer and use it in GitHub Desktop.
Save ftntming/4f9dbf5cdb5edf0db5e74f4550bc4307 to your computer and use it in GitHub Desktop.
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