I use golang 1.5.
I use the gopkg.in/rana/ora.v3 library to connect to Oracle.
In the example of Working With The Oracle Package Directly, there is an implementation of using the Oracle environment resource:
env, err := ora.OpenEnv(nil) defer env.Close() if err != nil { panic(err) } We see that in defer resource closure goes directly through the env link.
But what will happen if a failure occurs. In the OpenEnv implementation , we see that the error for env returns the value nil. With emit this situation:
package main import ( "gopkg.in/rana/ora.v3" "errors" ) func main() { defer fmt.Println("Hello") // env, err := ora.OpenEnv(nil) var env *ora.Env = nil var err error = errors.New("Test error") defer env.Close() if err != nil { panic(err) } } We get the following error message:
Hello
panic: Test error
panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code = 0x1 addr = 0x0 pc = 0x48753a]goroutine 1 [running]:
gopkg.in/rana/ora%2ev3.(*Env).Close(0x0, 0x0, 0x0)
And even though we see that the resulting error does not interrupt the execution of the remaining defer methods, but the error is still fixed.
Is it correct to leave it like this?
Or is it still worth ensuring that with evn == nil defer env.Close() will never be called?
Or in the body of the method passed to defer , should env be checked for nil ?
defer func () { if env != nil { env.Close() }}()