go操作redis

package main

import (
    "fmt"
    "github.com/gomodule/redigo/redis"
)

func main() {

    conn,err := redis.Dial("tcp","127.0.0.1:6379")
    if err != nil{
        fmt.Printf("connect to redis fail [%s]",err)
        return
    }

    //退出的时候关闭链接
    defer conn.Close()

    _ ,err = conn.Do("set","testKey","testVaule")
    if err != nil{
        fmt.Printf("redis opr fail [%s]",err)
        return
    }

    testVaule, err := redis.String(conn.Do("GET", "testKey"))

    if err != nil {
        fmt.Println("redis get failed:", err)
    } else {
        fmt.Printf("Get testKey: %v \n", testVaule)
    }
}

 

posted @ 2018-10-03 17:07  木易森林  阅读(284)  评论(0编辑  收藏