The more useful third-party open source Redisclient in Golang are:
- Go-redis
- Source Address: Https://github.com/go-redis/redis
- Document Address: Http://godoc.org/github.com/go-redis/redis
- Redigo
- Source Address: Https://github.com/gomodule/redigo
- Document Address: Http://godoc.org/github.com/gomodule/redigo/redis
Two are very good redisclient library, is also the Redis official on-line recommendation, the author chooses is Go-redis, because Go-redis encapsulates most of the Redis command, do not care about the details of the Redis command, directly call the corresponding interface on the line Redigo is command-based, sends a command, and then parses reply; So relatively speaking, I think Go-redis interface is more friendly;
Here also a little advertising, if a friend is in C + +, you can try the author of a C + + version of the redisclient:redis_cpp, similar to Go-redis, encapsulates the majority of REDIS commands, support Redis standalone, Sentinel, cluster mode, interface friendly and useful
1. Installation of Go-redis
go get https://github.com/go-redis/redis
2. Test code
Redis_test Project Main.gopackage mainimport ("FMT" "Time" "Github.com/go-redis/redis") func Err_handler (Err E Rror) {fmt. Printf ("Err_handler, error:%s\n", err. Error ()) Panic (err. Error ())}func standalone_single_client_redis_test () {fmt. Printf ("Standalone_single_client_redis_test") Client: = Redis. Newclient (&redis. options{Addr: "localhost:6379", Password: "", db:0,}) defer client. Close () Pong, err: = client. Ping (). Result () if err! = Nil {fmt. Printf ("Ping error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("Ping Result:%s\n", Pong) fmt. Printf ("----------------------------------------\ n")//Set/get test FMT. Printf ("Set/get test\n") Err = client. Set ("foo", "Bar", 0). ERR () if err! = Nil {fmt. Printf ("Try set Key[foo] to Value[bar] error[%s]\n", err. Error ()) Err_handler (err)} err = client. Set ("Foo1", "bar1", time. HOUR*2). ERR () if err! = Nil {Fmt. Printf ("Try set Key[foo1] to Value[bar1] error[%s]\n", err. Error ()) Err_handler (ERR)}//Get value value, err: = client. Get ("foo"). Result () if err! = Nil {fmt. Printf ("Try get Key[foo] error[%s]\n", err. Error ())//Err_handler (ERR)} FMT. Printf ("Key[foo" s value is%s\n ", value) value, Err = client. Get ("Foo1"). Result () if err! = Nil {fmt. Printf ("Try get Key[foo1] error[%s]\n", err. Error ())//Err_handler (ERR)} FMT. Printf ("Key[foo1" s value is%s\n ", value) value, Err = client. Get ("Foo2"). Result () if err! = Nil {fmt. Printf ("Try get Key[foo2] error[%s]\n", err. Error ())//Err_handler (ERR)} FMT. Printf ("Key[foo2] 's value is%s\n", value)//Get TTL duration, err: = client. TTL ("foo"). Result () if err! = Nil {fmt. Printf ("Try get ttl of Key[foo] error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("Key[foo] ' TTL is [%s]%fs\n", duration. String (), duration. SecondS ()) duration, err = client. TTL ("Foo1"). Result () if err! = Nil {fmt. Printf ("Try get ttl of key[foo1] error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("Key[foo1] ' TTL is [%s]%ds\n", duration. String (), Int64 (duration. Seconds ())) Fmt. Printf ("----------------------------------------\ n")//list Test FMT. Printf ("list test\n") Err = client. Rpush ("Tqueue", "TMSG1"). ERR () if err! = Nil {fmt. Printf ("Rpush List[tqueue] error[%s]\n", err. Error ()) Err_handler (Err)} List_len, err: = client. Llen ("Tqueue"). Result () if err! = Nil {fmt. Printf ("Try get len of List[tqueue] error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("Len of List[tqueue] is%d\n", list_len) result, err: = client. Blpop (time. Second*1, "Tqueue"). Result () if err! = Nil {fmt. Printf ("Blpop List[tqueue] error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("Blpop List[tqueue], value[%s]\n", result[1]) FMT. Printf ("----------------------------------------\ n") fmt. Printf ("Hmap test\n") Err = client. Hset ("TMap", "1", "F1"). ERR () if err! = Nil {fmt. Printf ("Try Hset Map[tmap] field[1] error[%s]\n", err. Error ()) Err_handler (err)} err = client. Hset ("TMap", "2", "F2"). ERR () if err! = Nil {fmt. Printf ("Try Hset Map[tmap] field[2] error[%s]\n", err. Error ()) Err_handler (Err)} Kv_map: = Make (map[string]interface{}) kv_map["3"] = "F3" kv_map["4"] = "F4 "Err = client." Hmset ("TMap", Kv_map). ERR () if err! = Nil {fmt. Printf ("Try Mset Map[tmap] field[3] field[4] error[%s]\n", err. Error ()) Err_handler (Err)} Map_len, err: = client. Hlen ("TMap"). Result () if err! = Nil {fmt. Printf ("Try get len of Map[tmap] error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("Len of Map[tmap] is%d\n", map_len)//Get Map value value, Err = client. Hget ("TMap", "1"). Result () if err! = Nil {fmt. Printf ("Try get field[1] value of Map[tmap] error[%s]\n", err. Error ()) Err_handler (Err)} FMT. Printf ("field[1] value of Map[tmap] is%s\n", value)//Hgetall result_kv, err: = client. Hgetall ("TMap"). Result () if err! = Nil {fmt. Printf ("Try Hgetall Map[tmap] error[%s]\n", err. Error ()) Err_handler (Err)} for K, V: = Range result_kv {fmt. Printf ("Map[tmap]%s =%s\n", K, V)} FMT. Printf ("----------------------------------------\ n") fmt. Printf ("PubSub test\n") PubSub: = client. Subscribe ("Test_channel") _, Err = PubSub. Receive () if err! = Nil {fmt. Printf ("Try subscribe Channel[test_channel] error[%s]\n", err. Error ()) Err_handler (ERR)}//Go channel to used to receive message ch: = PubSub. Channel ()//Publish a message Err = client. Publish ("Test_channel", "Hello"). ERR () if err! = Nil {fmt. Printf ("Try publish message to Channel[test_channel] error[%s]\n", err. Error ()) Err_handler (ERR)} time. Afterfunc (time. Second*2, Func () {_ = PubSub. Close ()})//consume message for {msg, OK: = <-ch if!ok {break} FM t.printf ("Recv message[%s" from channel[%s]\n ", Msg.) Payload, Msg. Channel)}}func Main () {FMT. Println ("redis_test!") Standalone_single_client_redis_test ()}