125func tempFilePath() string {
126 file, err := os.CreateTemp("", "")
127 if err != nil {
128 log.Fatal(err)129 }
130 os.Remove(file.Name())
131 return file.Name()
129 } else {
130 printError(err.Error())
131 }
132 os.Exit(1)133}
134
135func printError(text string) {
101 // run command
102 if err := rootCmd.Execute(); err != nil {
103 fmt.Println(err)
104 os.Exit(1)105 }
106
107 updateCancel() // if the update checker hasn't completed by now, abort it
133 }
134
135 waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus)
136 os.Exit(waitStatus.ExitStatus())137 return nil
138}
139
Packages exposing functions that can stop program execution by exiting are hard to reuse. Program exits in functions other than main() or init() are not encouraged.
func nonMainFunc() {
safelyExit := true // somehow arrive at this value
if safelyExit {
os.Exit(0)
}
}
func main() {
nonMainFunc()
}
func nonMainFunc() bool {
safelyExit := true // somehow arrive at this value
}
func main() {
safelyExit := nonMainFunc()
if safelyExit {
os.Exit(0)
}
}