golang连接redis

一、搭建redis-server

https://www.cnblogs.com/amuge/articles/16215516.html

二、golang支持redis的驱动有以下几个



github.com/astaxie/redisgo

三、简单实现连接goredis

3.1 安装
go mod init main.go
go get -u github.com/astaxie/goredis

3.2 操作代码:

package main

import (
	"fmt"
	"log"

	"github.com/astaxie/goredis"
)

func main() {
	var client goredis.Client
	client.Addr = "ip:port"

	client.Db = 13
	client.Password = "密码"

	err := client.Set("a", []byte("hello world"))
	if err != nil {
		log.Fatal(err)
	}

	val, _ := client.Get("a")
	fmt.Println(string(val))
}

四、连接goredis源码

https://github.com/astaxie/goredis/blob/master/redis.go

posted @ 2022-05-02 16:14  amu2018  阅读(112)  评论(0编辑  收藏  举报