Go Operation Redis

Source: Internet
Author: User
Tags redis server
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

    • go get github.com/alphazero/Go-RedisThat's the Go-redis bag.

Code Validation

    • Verify that the Redis service is turned on

    • redis.conf Note bind IP acknowledgement can be connected

    • Code

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.