Exit {.en}
出口 {.zh}
::: {.en}
Use os.Exit
to immediately exit with a given
status.
:::
::: {.zh}
使用os.Exit
立即退出givenstatus。
:::
package main
import "fmt"
import "os"
func main() {
::: {.en}
defer
s will not be run when using os.Exit
, so
this fmt.Println
will never be called.
:::
::: {.zh}
defer
s在使用os.Exit
时将不会运行,sothisfmt.Println
永远不会被调用。
:::
defer fmt.Println("!")
::: {.en} Exit with status 3. :::
::: {.zh}
退出状态3。
:::
os.Exit(3)
}
::: {.en}
Note that unlike e.g. C, Go does not use an integer
return value from main
to indicate exit status. If
you’d like to exit with a non-zero status you should
use os.Exit
.
:::
::: {.zh}
请注意,不像C,Go不使用main
中的整数返回值来表示退出状态。如果你想以非零状态退出,你应该使用os.Exit
。
:::
::: {.en}
If you run exit.go
using go run
, the exit
will be picked up by go
and printed.
:::
::: {.zh}
如果你使用go run
运行exit.go
,退出将被’go`选中并打印出来。
:::
$ go run exit.go
exit status 3
::: {.en} By building and executing a binary you can see the status in the terminal. :::
::: {.zh}
通过构建和执行二进制文件,您可以在终端中查看状态。
:::
$ go build exit.go
$ ./exit
$ echo $?
3
::: {.en}
Note that the !
from our program never got printed.
:::
::: {.zh}
请注意,我们程序中的!
从未打印过。
:::