From 641f72a558cfa62c933514e3558bdd02b2001d48 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Thu, 2 Mar 2023 19:28:51 +0100 Subject: [PATCH] Avoid os.Exit() in function with deferred actions. --- main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 53e5ec7..39cde11 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,10 @@ import ( ) func main() { + os.Exit(main_body()) +} + +func main_body() int { var conf_file string // Parse args and read config @@ -27,14 +31,14 @@ func main() { config, err := getConfig(conf_file) if err != nil { fmt.Println("Error reading config file " + conf_file) - os.Exit(1) + return 1 } // Open logfile logfile, err := os.OpenFile(config.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { fmt.Println("Error opening log file " + config.LogPath + ".") - os.Exit(2) + return 2 } defer logfile.Close() @@ -67,7 +71,8 @@ func main() { http_server.Shutdown(context.Background()) https_server.Shutdown(context.Background()) case err := <-errs: - log.Fatal(err) + log.Println("Fatal: " + err.Error()) } + return 0 }