如何在 Go 中实现 PHP 函数 `die()`(或 `exit()`)?
How to implement a PHP function `die()` (or `exit()`) in Go?
在PHP中,die()
用于停止运行脚本以防止意外行为。在 Go 中,结束句柄函数的惯用方式是什么? panic()
或 return
?
您可以在 HTTP 处理程序中使用 panic
。服务器会处理它。参见 Handler。
If ServeHTTP panics, the server (the caller of ServeHTTP) assumes that the effect of the panic was isolated to the active request. It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
函数panic
是为程序无法继续的情况保留的。无法只处理一个请求与无法继续工作不同,所以我会记录错误,设置正确的 HTTP 状态并使用 return
。参见 Effective Go。
The usual way to report an error to a caller is to return an error as an extra return value. The canonical Read method is a well-known instance; it returns a byte count and an error. But what if the error is unrecoverable? Sometimes the program simply cannot continue.
在 Go 中打破函数的惯用方法是使用 panic()
。这是在运行时停止执行事件的实际方法。如果你想恢复恐慌,你可以使用内置的 recover()
函数。
恐慌 解释:
Panic is a built-in function that stops the ordinary flow of control
and begins panicking. When the function F calls panic, execution of F
stops, any deferred functions in F are executed normally, and then F
returns to its caller.
https://blog.golang.org/defer-panic-and-recover
恢复 说明:
Recover is a built-in function that regains control of a panicking
goroutine. Recover is only useful inside deferred functions. During
normal execution, a call to recover will return nil and have no other
effect. If the current goroutine is panicking, a call to recover will
capture the value given to panic and resume normal execution.
https://blog.golang.org/defer-panic-and-recover
这是一个简单的例子:
package main
import "fmt"
func badCall() {
panic("Bad call happend!")
}
func test() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Panicking %s\n\r", err)
}
}()
badCall()
fmt.Println("This is never executed!!")
}
func main() {
fmt.Println("Start testing")
test()
fmt.Println("End testing")
}
如果您不想在退出程序后打印堆栈跟踪,您可以使用os.Exit
。您还可以使用 os.Exit
.
设置特定的退出代码
示例 (https://play.golang.org/p/XhDkKMhtpm):
package main
import (
"fmt"
"os"
)
func foo() {
fmt.Println("bim")
os.Exit(1)
fmt.Println("baz")
}
func main() {
foo()
foo()
}
另请注意,os.Exit
会立即停止程序并且不会 运行 任何延迟函数,而 panic()
会。参见 https://play.golang.org/p/KjGFZzTrJ7 and https://play.golang.org/p/Q4iciT35kP。
你应该使用 os.Exit
。
Exit causes the current program to exit with the given status code.
Conventionally, code zero indicates success, non-zero an error. The
program terminates immediately; deferred functions are not run.
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Start")
os.Exit(1)
fmt.Println("End")
}
甚至,你可以使用panic
,它也会停止正常执行,但在执行停止时抛出错误。
The panic built-in function stops normal execution of the current
goroutine. When a function F calls panic, normal execution of F stops
immediately. Any functions whose execution was deferred by F are run
in the usual way, and then F returns to its caller. To the caller G,
the invocation of F then behaves like a call to panic, terminating G's
execution and running any deferred functions. This continues until all
functions in the executing goroutine have stopped, in reverse order.
At that point, the program is terminated and the error condition is
reported, including the value of the argument to panic. This
termination sequence is called panicking and can be controlled by the
built-in function recover.
package main
import "fmt"
func main() {
fmt.Println("Start")
panic("exit")
fmt.Println("End")
}
在PHP中,die()
用于停止运行脚本以防止意外行为。在 Go 中,结束句柄函数的惯用方式是什么? panic()
或 return
?
您可以在 HTTP 处理程序中使用 panic
。服务器会处理它。参见 Handler。
If ServeHTTP panics, the server (the caller of ServeHTTP) assumes that the effect of the panic was isolated to the active request. It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
函数panic
是为程序无法继续的情况保留的。无法只处理一个请求与无法继续工作不同,所以我会记录错误,设置正确的 HTTP 状态并使用 return
。参见 Effective Go。
The usual way to report an error to a caller is to return an error as an extra return value. The canonical Read method is a well-known instance; it returns a byte count and an error. But what if the error is unrecoverable? Sometimes the program simply cannot continue.
在 Go 中打破函数的惯用方法是使用 panic()
。这是在运行时停止执行事件的实际方法。如果你想恢复恐慌,你可以使用内置的 recover()
函数。
恐慌 解释:
Panic is a built-in function that stops the ordinary flow of control and begins panicking. When the function F calls panic, execution of F stops, any deferred functions in F are executed normally, and then F returns to its caller.
https://blog.golang.org/defer-panic-and-recover
恢复 说明:
Recover is a built-in function that regains control of a panicking goroutine. Recover is only useful inside deferred functions. During normal execution, a call to recover will return nil and have no other effect. If the current goroutine is panicking, a call to recover will capture the value given to panic and resume normal execution.
https://blog.golang.org/defer-panic-and-recover
这是一个简单的例子:
package main
import "fmt"
func badCall() {
panic("Bad call happend!")
}
func test() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Panicking %s\n\r", err)
}
}()
badCall()
fmt.Println("This is never executed!!")
}
func main() {
fmt.Println("Start testing")
test()
fmt.Println("End testing")
}
如果您不想在退出程序后打印堆栈跟踪,您可以使用os.Exit
。您还可以使用 os.Exit
.
示例 (https://play.golang.org/p/XhDkKMhtpm):
package main
import (
"fmt"
"os"
)
func foo() {
fmt.Println("bim")
os.Exit(1)
fmt.Println("baz")
}
func main() {
foo()
foo()
}
另请注意,os.Exit
会立即停止程序并且不会 运行 任何延迟函数,而 panic()
会。参见 https://play.golang.org/p/KjGFZzTrJ7 and https://play.golang.org/p/Q4iciT35kP。
你应该使用 os.Exit
。
Exit causes the current program to exit with the given status code. Conventionally, code zero indicates success, non-zero an error. The program terminates immediately; deferred functions are not run.
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("Start")
os.Exit(1)
fmt.Println("End")
}
甚至,你可以使用panic
,它也会停止正常执行,但在执行停止时抛出错误。
The panic built-in function stops normal execution of the current goroutine. When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by F are run in the usual way, and then F returns to its caller. To the caller G, the invocation of F then behaves like a call to panic, terminating G's execution and running any deferred functions. This continues until all functions in the executing goroutine have stopped, in reverse order. At that point, the program is terminated and the error condition is reported, including the value of the argument to panic. This termination sequence is called panicking and can be controlled by the built-in function recover.
package main
import "fmt"
func main() {
fmt.Println("Start")
panic("exit")
fmt.Println("End")
}