Exit in Clojure
Here’s an idiomatic Clojure example demonstrating the concept of exiting a program:
This Clojure program demonstrates how to exit a program with a specific status code. Let’s break down the key components:
We define a
cleanup
function that simulates a cleanup operation. In a real-world scenario, this might involve closing file handles or database connections.The
exit-program
function is our custom exit function. It prints the exit status and then callsSystem/exit
with the given status code. This is equivalent toos.Exit
in Go.In the
-main
function, we useaddShutdownHook
to register our cleanup function. This is similar to usingdefer
in Go, but it’s important to note that shutdown hooks are not guaranteed to run when usingSystem/exit
.We then call
exit-program
with a status of 3, which will immediately terminate the program.
To run this program:
- Save the code in a file named
exit_example.clj
. - Run it using the Clojure command-line tool:
To check the exit status in a shell:
Note that the cleanup function is not called due to the use of System/exit
. This behavior is similar to Go’s os.Exit
, which also doesn’t run deferred functions.
In Clojure, it’s generally preferred to let the program exit normally rather than using System/exit
. This allows for proper resource cleanup and is considered more idiomatic. However, System/exit
is available when you need to forcibly terminate the program with a specific exit code.
Remember that in Clojure, like in Go, the return value of the -main
function is not used to determine the exit status. To set a non-zero exit status, you must use System/exit
explicitly.