If I have this scenario (in pseudocode):
func main() {
defer atTheEnd()
someSuch()
}
func someSuch() {
os.Exit(1)
}
Does the exit happen immediately in someSuch() or does the deferred
atTheEnd() still end up being called?
Thanks!
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Advanced Qt Programming" - ISBN 0321635906
http://www.qtrac.eu/aqpbook.html
"Deferred function calls are executed in LIFO order immediately before
the surrounding function returns, after the return values, if any,
have been evaluated, but before they are returned to the caller."
http://golang.org/doc/go_spec.html#Defer_statements
os.Exit(1) doesn't return, so someSuch() doesn't return, so main()
doesn't return so the
defer is never called.
- jessta
--
=====================
http://jessta.id.au
of course I should have tried it in the sandbox:-)
However, I do think the os.Exit() docs might benefit from some addition.
At present the text is:
Exit causes the current program to exit with the given status code.
Conventionally, code zero indicates success, non-zero an error.
Maybe it could be added to---maybe something like:
Exit causes the current program to exit with the given status code.
Conventionally, code zero indicates success, non-zero an error.
On exit, any running goroutines are terminated and any allocated
memory is freed by the garbage collector. No pending deferred
statements are executed, so although any open files will be closed
by the Go runtime system, resources outside Go's control, such as
open database or network connections, will not be closed.
... if that's true of course.
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Python 3" - ISBN 0321680561
http://www.qtrac.eu/py3book.html
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Python 3" - ISBN 0321680561
http://www.qtrac.eu/py3book.html
os.Exit(1) doesn't return, so someSuch() doesn't return, so main()
doesn't return so the defer is never called.
Since os.Exit() exits immediately and no deferred statments can exit,
it seems to me that if you have at least one deferred statement (that
you want to be executed before termination) then there is no way to
return non-zero to the OS.
func main() {
...
defer closeMyDatabaseConnection() // this is vital
...
// uh oh something went wrong & I need to terminate with non-zero to
// indicate this, but I can't call os.Exit(2) because then my
// database connection will be left hanging.
}
Or is there something in the library I've missed that I could use?
Some languages have an atexit() function that can handle such
cases---does Go have something similar?
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
if you really wanted to do this, it would be straightforward;
var exitCode int
func exit() {
os.Exit(exitCode)
}
func main() {
defer exit()
defer closeMyDatabaseConnection() // this is vital
// uh oh something went wrong & I need to terminate with non-zero to
// indicate this
exitCode = 2
}
or
defer func() { closeMyDatabaseConnection(); os.Exit( exitCode ) }
I think.
Chris
--
Chris "carthorse" Dollin
sure. i was assuming the database closing could be in some other
function though.
That's nice; I'll use that approach. Thanks!
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Rapid GUI Programming with Python and Qt" - ISBN 0132354187
http://www.qtrac.eu/pyqtbook.html