os.Exit
The Go function that ends the program immediately with the exit code you give it, such as 0 for success.
In more detail
Deferred functions do not run when you call os.Exit, so call it only from main, after cleanup. Pick codes deliberately and document them: the course uses 0 for success, 1 for a failed check and 2 for a misused command line.
Example
os.Exit(exitBadArgs)Related terms
- exit code — A number a process returns when it finishes: 0 means success, anything else means something went wrong.
- defer — A Go statement that schedules a function call to run when the surrounding function returns, used for cleanup such as closing a file.
- package main — The special package name that makes a Go program executable, together with a function called main where the program starts.
Taught in
- Your First Program and go run — Concurrent Systems Programming with Go
- Files and the os Package — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go