Skip to content

Instantly share code, notes, and snippets.

@genedna
Created March 8, 2019 10:47
Show Gist options
  • Save genedna/a6c72fedc7b96b2401921ab808b917e4 to your computer and use it in GitHub Desktop.
Save genedna/a6c72fedc7b96b2401921ab808b917e4 to your computer and use it in GitHub Desktop.
go-redis pub sub example
package main
import (
"github.com/go-redis/redis"
)
var (
RedisClient *redis.Client
)
func main() {
RedisClient = redis.NewClient(&redis.Options{
Addr: Env.Redis.Host + ":" + Env.Redis.Port,
Password: Env.Redis.Password,
DB: Env.Redis.Db,
})
// MULTIPLES REPETITION STRUCTURES INSIDE GO ROUTINES
go func(){
psNewMessage := RedisClient.Subscribe("channel1")
for {
msg, _ := psNewMessage.ReceiveMessage()
go func() {
// DO SOMETHING WITH msg
}()
}
}()
go func(){
psCallInPulse := RedisClient.Subscribe("channel2")
for {
msg, _ := psCallInPulse.ReceiveMessage()
go func() {
// DO SOMETHING WITH msg
}()
}
}()
// MORE 7 GO ROUTINES
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment