我需要使用defer
来释放使用C
库手动创建的分配,但我也需要在某个时刻使用非0状态的os.Exit
.棘手的是os.Exit
会跳过任何延迟的指令:
package main
import "fmt"
import "os"
func main() {
// `defer`s will _not_ be run when using `os.Exit`, so
// this `fmt.Println` will never be called.
defer fmt.Println("!")
// sometimes ones might use defer to do critical operations
// like close a database, remove a lock or free memory
// Exit with status code.
os.Exit(3)
}
playground :https://gobyexample.com/exit个playground 中有http://play.golang.org/p/CDiAh9SXRM个被盗
那么,如何退出一个Go程序来兑现声明的defer
个呼叫呢?除了os.Exit
,还有其他 Select 吗?