Golang连接redis
代码
package main import ( "fmt" "github.com/alphazero/Go-Redis" ) func main() { //DefaultSpec()创建一个连接 //选择host,若需要auth,则password填写 //spec := redis.DefaultSpec().Host("192.168.1.111").Db(0).Password(""); //spec := redis.DefaultSpec().Host("127.0.0.1").Port(6379).Db(0).Password(""); //完整版 //若连接的本机redis-server,则host可以省略 spec := redis.DefaultSpec().Db(0).Password(""); client, err := redis.NewSynchClientWithSpec (spec); if err != nil { fmt.Println("Connect redis server fail") return } dbkey := "test"; value :=[]byte("Hello world!") client.Set(dbkey, value) getValue ,err:= client.Get(dbkey) if err != nil { fmt.Println("Get Key fail") return } else { str := string(getValue) fmt.Println(str) } }
转换成byte存入redisvalue :=[]byte("Hello world!")
从redis中取出,再转换成string:
getValue ,err:= client.Get(dbkey)
str := string(getValue)
参考链接:
[dm href=’http://www.tuicool.com/articles/zqq6Nb2′]推酷[/dm][dm href=’http://blog.csdn.net/cinience/article/details/11354079′]CSDN[/dm]