安装redis测试服务器
1、代码
package main import ( "fmt" "gopkg.in/redis.v4" ) func main() { client := createClient() defer client.Close() err := client.Set("name", "zs", 0).Err() if err != nil { panic(err) } val, err := client.Get("name").Result() if err != nil { panic(err) } err = client.Set("age", 10, 0).Err() val, err = client.Get("age").Result() if err != nil { panic(err) } fmt.Println("age", val) } // 创建 redis 客户端 func createClient() *redis.Client { client := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", DB: 0, PoolSize: 5, }) pong, err := client.Ping().Result() fmt.Println(pong, err) return client }
向redis读写一个字符串和数值,运行结果