Go - redis

Go-Redis包管理

  • 很明顯,咱們Go是沒有這個包的,那麼咱們能夠利用GO的命令。首先咱們得先配置GOPATH的路徑,我選擇的是/data/go/goSource做爲我包的路徑,因此shell執行以下export GOPATH=/data/go/goSourcegit

  • 由於咱們須要到github上面找類庫資源,也必須安裝git yum install gitgithub

  • go get github.com/alphazero/Go-Redis這樣就載下了Go-Redis包了redis

代碼驗證

  • 確認redis服務已經開啓shell

  • redis.conf注意配置code

  • codeserver

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("");
    //若鏈接的本機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);
    }
    
}

總結

  • 咱們不難發現,存到redis,是byte,取的值也是byte。用的時候,須要作相關轉換。資源

  • 固然今天寫的比較少,只是基礎的String ,其餘數據類型操做,好比hash,client.Hset(dbkey, property, value).操做方法和其餘語言一致。注意首字母大寫get

相關文章
相關標籤/搜索