https://github.com/go-redis/redis

    1. import (
    2. "context"
    3. "fmt"
    4. "github.com/go-redis/redis/v8"
    5. )
    6. var ctx = context.Background()
    7. func main() {
    8. rdb := redis.NewClient(&redis.Options{
    9. Addr: "192.168.31.151:6379",
    10. Password: "314159", // no password set
    11. DB: 0, // use default DB
    12. })
    13. err := rdb.Set(ctx, "key", "value", 0).Err()
    14. if err != nil {
    15. panic(err)
    16. }
    17. val, err := rdb.Get(ctx, "key").Result()
    18. if err != nil {
    19. panic(err)
    20. }
    21. fmt.Println("key", val)
    22. }
    1. func main() {
    2. pubsub := rdb.Subscribe(ctx, "mychannel1")
    3. // Wait for confirmation that subscription is created before publishing anything.
    4. _, err := pubsub.Receive(ctx)
    5. if err != nil {
    6. panic(err)
    7. }
    8. // Publish a message.
    9. err = rdb.Publish(ctx, "mychannel1", "hello").Err()
    10. if err != nil {
    11. panic(err)
    12. }
    13. // Go channel which receives messages.
    14. ch := pubsub.Channel()
    15. time.AfterFunc(time.Second, func() {
    16. // When pubsub is closed channel is closed too.
    17. _ = pubsub.Close()
    18. })
    19. // Consume messages.
    20. for msg := range ch {
    21. fmt.Println(msg.Channel, msg.Payload)
    22. }
    23. }