Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe close connection in Golang

Tags:

go

panic

recover

When I open a socket connection, I immediately put the socket.Close() logic in a defer function after the opening of the socket. However, what if the socket.Close() would cause another panic? Should I always nest another defer/recover inside the outer defer to prevent my program from crashing? Something like this: http://play.golang.org/p/GnEMQS-0jj

Thanks, Elgs

like image 708
Qian Chen Avatar asked Dec 10 '13 13:12

Qian Chen


1 Answers

Generally you don't need to worry much about panics. They usually represent two classes of errors: developer mistakes (nil references, array out of bounds) and system level errors you probably can't do much about (like running out of memory).

As others said socket.Close will not panic, rather it returns an error. If you do:

defer socket.Close()

The error is discarded and you don't need to do anything else.

But suppose you did want to recover from a panic. If you're recovery handler is deferred first then you don't need to do anything else:

func main() {
  defer func() {
    if err := recover(); err != nil {
      fmt.Println(err)
    }
  }()
  defer panic("this will be recovered")
}

Deferred functions are run in reverse order: http://golang.org/ref/spec#Defer_statements

Deferred functions are executed immediately before the surrounding function returns, in the reverse order they were deferred.

like image 72
Caleb Avatar answered Sep 24 '22 05:09

Caleb