Created
November 23, 2020 05:32
-
-
Save irshadhasmat/abdd99a0126c1971a6bbe3b68a9f37ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"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