Google App Engine-Redislabs Go运行时生产错误-无效的内存地址或nil指针取消引用

Google App Engine-Redislabs Go运行时生产错误-无效的内存地址或nil指针取消引用

问题描述:

I'm using the Redis Cloud service (from Redis Labs) on Google App engine Go Runtime , and I get the above mentioned error when I try getting a key that doesn't exist. The code works fine on the local test server, but panics in production.

 c, err := redis.Dial("tcp", "pub-redis-myredis:<myport>")
_, err = c.Do("AUTH", "password") 
value, err := c.Do("GET", "foo4")

if value == nil {
    log.Infof(contextOfAppEngineWhereServerIsRunning, "value not found in redislabs")

} 

The log shows that the panic is in the line _, err = c.Do("AUTH", "password")

On AppEngine your application (webapp) runs in a sandboxed environment where you can't use the standard net package for sockets (which I assume redislabs use).

You have to use the appengine/socket package for outbound network sockets.

It most likely works locally because the restriction only applies in production environment.

Always check returned error values. Currently in the code you posted you check none. Should you have checked it, you would see the cause from the error.

You get invalid memory address or nil pointer dereference error because your first call fails:

c, err := redis.Dial("tcp", "pub-redis-myredis:<myport>")

err will be a non-nil value and c is nil which means you can't call it's Do() method.