exit
调用 os
包,自定义 status code
。
例子
package mainimport "os"func main() { os.Exit(3) println("exiting ...") // 不会执行到这里}// $ go run main.go// 输入如下 /** exit status 3*/
过程 id
调用 os
包即可。
例子
package mainimport ( "fmt" "os")func main() { fmt.Printf("Process ID = %d\n", os.Getpid()) fmt.Printf("Parent process ID = %d\n", os.Getppid())}// $ go run main.go// 输入如下,你的输入可能和这里的不一样/** Process ID = 13962 Parent process ID = 13860*/
信号
调用 os/signal
包即可。
例子
package mainimport ( "fmt" "os" "os/signal")func main() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) fmt.Println("程序执行中... 按 Ctrl + C 终止执行") <-c // 期待信号被触发 fmt.Println("程序执行终止")}// $ go run main.go// 输入如下 /** 程序执行中... 按 Ctrl + C 终止执行 ^C程序执行终止*/
限度
SIGKILL
信号 和 SIGSTOP
信号无奈被捕捉,这是内核的限度, 目标是为了让操作系统管制过程的生命周期。
执行命令
调用 os/exec
包即可。
例子
package mainimport ( "fmt" "os/exec")func main() { out, err := exec.Command("date").Output() if err != nil { panic(err) } fmt.Printf("%s\n", out) out, err = exec.Command("git", "--version").Output() if err != nil { panic(err) } fmt.Printf("%s\n", out)}// $ go run main.go// 输入如下,你的输入可能和这里的不一样/** Thu Nov 3 08:14:57 CST 2022 git version 2.30.1 (Apple Git-130)*/