diff --git a/v2/example_test.go b/v2/example_test.go index df3c301..721392e 100644 --- a/v2/example_test.go +++ b/v2/example_test.go @@ -7,6 +7,14 @@ import ( "github.com/vmihailenco/redis/v2" ) +var client *redis.Client + +func init() { + client = redis.NewTCPClient(&redis.Options{ + Addr: ":6379", + }) +} + func ExampleNewTCPClient() { client := redis.NewTCPClient(&redis.Options{ Addr: "localhost:6379", @@ -32,27 +40,17 @@ func ExampleNewUnixClient() { } func ExampleClient() { - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - set := client.Set("foo", "bar") fmt.Println(set.Val(), set.Err()) - get := client.Get("foo") - fmt.Println(get.Val(), get.Err()) + get := client.Get("hello") + fmt.Printf("%q %s %v", get.Val(), get.Err(), get.Err() == redis.Nil) // Output: OK - // bar + // "" redis: nil true } func ExampleClient_Pipelined() { - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - cmds, err := client.Pipelined(func(c *redis.Pipeline) { c.Set("key1", "hello1") c.Get("key1") @@ -64,21 +62,16 @@ func ExampleClient_Pipelined() { } func ExamplePipeline() { - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - pipeline := client.Pipeline() set := pipeline.Set("key1", "hello1") - get := pipeline.Get("key2") + get := pipeline.Get("key1") cmds, err := pipeline.Exec() fmt.Println(cmds, err) fmt.Println(set) fmt.Println(get) - // Output: [SET key1 hello1: OK GET key2: (nil)] (nil) + // Output: [SET key1 hello1: OK GET key1: hello1] // SET key1 hello1: OK - // GET key2: (nil) + // GET key1: hello1 } func ExampleMulti() { @@ -95,11 +88,6 @@ func ExampleMulti() { }) } - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - client.Del("key") tx := client.Multi() @@ -123,11 +111,6 @@ func ExampleMulti() { } func ExamplePubSub() { - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - pubsub := client.PubSub() defer pubsub.Close() @@ -148,11 +131,6 @@ func ExamplePubSub() { } func ExampleScript() { - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - setnx := redis.NewScript(` if redis.call("get", KEYS[1]) == false then redis.call("set", KEYS[1], ARGV[1]) @@ -182,12 +160,7 @@ func Example_customCommand() { return cmd } - client := redis.NewTCPClient(&redis.Options{ - Addr: ":6379", - }) - defer client.Close() - get := Get(client, "key_does_not_exist") fmt.Printf("%q %s", get.Val(), get.Err()) - // Output: "" (nil) + // Output: "" redis: nil } diff --git a/v2/parser.go b/v2/parser.go index 4d20dd6..6d60b83 100644 --- a/v2/parser.go +++ b/v2/parser.go @@ -19,7 +19,7 @@ const ( ) // Redis nil reply. -var Nil = errors.New("(nil)") +var Nil = errors.New("redis: nil") // Redis transaction failed. var TxFailedErr = errors.New("redis: transaction failed") diff --git a/v2/redis_test.go b/v2/redis_test.go index 207f122..981cad5 100644 --- a/v2/redis_test.go +++ b/v2/redis_test.go @@ -267,7 +267,7 @@ func (t *RedisTest) TestCmdStringMethod(c *C) { func (t *RedisTest) TestCmdStringMethodError(c *C) { get2 := t.client.Get("key_does_not_exists") - c.Assert(get2.String(), Equals, "GET key_does_not_exists: (nil)") + c.Assert(get2.String(), Equals, "GET key_does_not_exists: redis: nil") } func (t *RedisTest) TestRunWithouthCheckingErrVal(c *C) {