Redigo:在尝试连接到连接池时出现无效的内存地址或空指针解引用。

huangapple go评论82阅读模式
英文:

Redigo: Invalid memory address or nil pointer dereference while attempting to connect to pool

问题

> Redis连接中.....!恐慌:运行时错误:无效的内存地址或空指针解引用[信号0xb code=0x1 addr=0x28 pc=0x40154b] goroutine 1 [运行中]: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", "Redis服务器地址")
	maxConnections = flag.Int("max-connections", 10, "Redis最大连接数")
)

func main() {

	//Redis连接
	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连接中...!")
	con := redisPool.Get()
	status, errStatus := con.Do("SET", "Name", "BookMyShow")
	if errStatus != nil {
		fmt.Println(errStatus)
	} else {
		fmt.Println("Redis已连接")
	}
	statusInsertion, _ := redis.String(status, errStatus)
	fmt.Println("插入状态:" + statusInsertion)
	value, _ := redis.String(con.Do("GET", "Name"))
	fmt.Println("检索到的值:" + value)
}

这是一个使用Go语言编写的Redis连接代码。它尝试连接到指定的Redis服务器,并执行一些操作。在你提供的代码中,出现了一个运行时错误,导致程序崩溃。你可能需要检查代码中的错误,并确保Redis服务器的连接设置正确。

英文:

> 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)
}

答案1

得分: 5

你犯了一个错误,是在使用返回值之后才检查错误:

con, err := redis.Dial("tcp", *redisAddress)
con.Do("SELECT", 1) // 在这里你正在使用可能为nil的con
if err != nil {
    return nil, err
}

请重新排列代码:

con, err := redis.Dial("tcp", *redisAddress)
if err != nil {
    return nil, err
}
con.Do("SELECT", 1) // 在检查错误之后再使用con
英文:

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

答案2

得分: 0

con, err := redis.Dial("tcp", *redisAddress)
if err != nil {
return nil, err
}
con.Do("SELECT", 0) //选择0号数据库,用于Nutcracker

英文:
con, err := redis.Dial("tcp", *redisAddress)
if err != nil {
    return nil, err
}
con.Do("SELECT", 0) //Select 0 for Nutcracker

huangapple
  • 本文由 发表于 2015年8月5日 16:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/31826666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定