This is a creation in Article, where the information may have evolved or changed.
Start
In the SF article, there are a lot of articles about Php,python operating redis. But there's less go-to-redis operations. Recently also just started to learn go, because of the preference for Redis, also by the way to learn a bit, and summed it down.
Go-redis Package Management
Obviously, we don't have this package for go, so we can use the Go command. First we have to configure GOPATH
the path, I chose/data/go/gosource as my package path, so the shell executes the following
export GOPATH=/data/go/goSource
Code Validation
package mainimport ( "fmt" "github.com/alphazero/Go-Redis")func main() { //DefaultSpec()创建一个连接 //选择host,若需要auth,则password填写 //spec := redis.DefaultSpec().Host("192.168.1.111").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); } }
Summarize
-
It is not difficult to find that, in Redis, it is byte and the value is byte. When you use it, you need to do the relevant conversion.
-
Of course today writes less, just the underlying string, other data type operations such as hash, client. Hset (Dbkey, property, Value)
. The method of operation is consistent with other languages. Note First letter capitalization