- Exit
- os.Exit
- %ERRORLEVEL%
- $?
Exit early with exit code
You will also have to pass a number to this function which is going to be the exit-code of your program. If you did not explicitly call os.Exit() then go will automatically set the exit-code to be 0. That means success.
In general exit-code 0 means success. Any other number indicates failure. Which number indicates which failure is totally up to you as a programmer.
The exit code is not printed anywhere on the screen, it is sort-of invisible, but the user of your program can check it by looking at the value of the $? variable on Unix/Linux/Mac systems, or the %ERRORLEVEL% variable on MS Windows systems.
examples/exit/code.go
package main import ( "fmt" "os" ) func main() { if len(os.Args) < 3 { fmt.Printf("Usage: %s PARAM PARAM\n", os.Args[0]) os.Exit(2) } fmt.Println(os.Args) }
echo $0 echo %ERRORLEVEL%