站长博客
站长博客随手笔记
Toggle navigation
站长博客
Home
MacOS
Database
Linux
PHP
Git
Golang
About Me
Archives
Tags
Go语言:go-redis客户端之消息的订阅subscribe与发布publish
2021-07-22 14:23:20
327
0
0
admin
订阅subscribe package main import ( "fmt" "github.com/go-redis/redis" ) func main() { client := redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", Password: "", DB: 0, }) pubsub := client.Subscribe("chat") defer pubsub.Close() for msg := range pubsub.Channel() { fmt.Printf("channel=%s message=%s\n", msg.Channel, msg.Payload) } } 订阅psubscribe package main import ( "fmt" "github.com/go-redis/redis" ) func main() { client := redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", Password: "", DB: 0, }) pubsub := client.PSubscribe("*") defer pubsub.Close() for msg := range pubsub.Channel() { fmt.Printf("channel=%s message=%s\n", msg.Channel, msg.Payload) } } 发布publish package main import ( "fmt" "github.com/go-redis/redis" ) func main() { client := redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", Password: "", DB: 0, }) n, err := client.Publish("chat", "hello").Result() if err != nil{ fmt.Printf(err.Error()) return } fmt.Printf("%d clients received the message\n", n) }
Prev:
Windows10子系统(WSL)修改安装目录
Next:
[参考]彻底解决Golang获取当前项目绝对路径问题
0
likes
327
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Table of content