Reputation: 378
Redis Connecting.....! panic: runtime error: invalid memory address or
nil pointer dereference [signal 0xb code=0x1 addr=0x28 pc=0x40154b]
goroutine 1 [running]: runtime.panic(0x52c6e0, 0x6b6348)
/usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6
main.func·001(0xc21005102c, 0x0, 0x4bcd4c, 0x1)
/home/vinay10949/redischeck/redischeck.go:21 +0x10b
github.com/garyburd/redigo/redis.(*Pool).get(0xc210051000, 0x0,
0xc2100378f0, 0x42dbdf, 0x7fbe9c177070) /usr/lib/go/src/pkg/github.com/garyburd/redigo/redis/pool.go:250
+0x3a2 github.com/garyburd/redigo/redis.(*Pool).Get(0xc210051000, 0x1, 0x1) > /usr/lib/go/src/pkg/github.com/garyburd/redigo/redis/pool.go:150
+0x27 main.main() /home/vinay10949/redischeck/redischeck.go:29 +0x13e
package main
import (
"flag"
"fmt"
"github.com/garyburd/redigo/redis"
//"reflect"
)
var (
redisAddress = flag.String("10.12.2.121", "10.12.2.121:6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
)
func main() {
//Redis Connection
redisPool := redis.NewPool(func() (redis.Conn, error) {
con, err := redis.Dial("tcp", *redisAddress)
con.Do("SELECT", 1)
if err != nil {
return nil, err
}
return con, err
}, *maxConnections)
fmt.Println("Redis Connecting...!")
con := redisPool.Get()
status, errStatus := con.Do("SET", "Name", "BookMyShow")
if errStatus != nil {
fmt.Println(errStatus)
} else {
fmt.Println("Redis Connected")
}
statusInsertion, _ := redis.String(status, errStatus)
fmt.Println("Status of Insertion :" + statusInsertion)
value, _ := redis.String(con.Do("GET", "Name"))
fmt.Println("Value Retrieved : " + value)
}
Upvotes: 0
Views: 2750
Reputation: 378
con, err := redis.Dial("tcp", *redisAddress)
if err != nil {
return nil, err
}
con.Do("SELECT", 0) //Select 0 for Nutcracker
Upvotes: 0
Reputation: 77945
You are making the mistake of checking for an error after using the returned value:
con, err := redis.Dial("tcp", *redisAddress)
con.Do("SELECT", 1) // Here you are using con which most likely is nil
if err != nil {
return nil, err
}
Rearrange the code to:
con, err := redis.Dial("tcp", *redisAddress)
if err != nil {
return nil, err
}
con.Do("SELECT", 1) // con should be used after checking for errors
Upvotes: 5