The Go language operates on redis usage instances
- 2020-05-19 04:59:55
- OfStack
This article illustrates the use of redis for the Go language operation. Share with you for your reference. The details are as follows:
package main
import (
"fmt"
"log"
"redis"
)
func main() {
//DefaultSpec() create 1 Individual connection specifications
spec := redis.DefaultSpec().Db(0).Password("");
// create 1 A new one syncClient And connect to Redis Is used on the server , The specified ConnectionSpec Interface.
client, err := redis.NewSynchClientWithSpec (spec);
if err != nil {
log.Println (" Connection to server failed >", err);
return
}
dbkey := "GAME:TEST:info";
value, err := client.Get(dbkey);
if err!= nil {
log.Println ("error on Get", err);
return
}
//
if value == nil {
value :=[]byte("Hello world!");
client.Set(dbkey, value);
fmt.Printf(" Insert data >%s \n",value)
} else {
fmt.Printf(" Received data >%s \n",value);
//return;
}
}
I hope this article has been helpful to your programming of Go language.