Go by Example: Exit

Usa os.Exit per uscire immediatamente con un status dato.

package main
import (
    "fmt"
    "os"
)
func main() {

I defer non verranno eseguiti quando si usa os.Exit, quindi questo fmt.Println non verrĂ  mai chiamato.

    defer fmt.Println("!")

Esci con status 3.

    os.Exit(3)
}

Nota che a differenza di es. C, Go non usa un valore di ritorno intero da main per indicare lo status di uscita. Se vuoi uscire con uno status diverso da zero dovresti usare os.Exit.

Se esegui exit.go usando go run, l’exit verrĂ  catturato da go e stampato.

$ go run exit.go
exit status 3

Compilando ed eseguendo un binario puoi vedere lo status nel terminale.

$ go build exit.go
$ ./exit
$ echo $?
3

Nota che il ! del nostro programma non è mai stato stampato.