From c4236dd1640f77654230669bcaa1fb613a432443 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Tue, 21 May 2024 20:15:04 +0200 Subject: [PATCH] Use deferred file close. --- main.go | 6 +----- parseconfig.go | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 1697333..d51bfda 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ func readMessage(messageFilePath string) (string, error) { if err != nil { return output, fmt.Errorf("readMessage: %w", err) } + defer file.Close() scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) for scanner.Scan() { @@ -73,11 +74,6 @@ func readMessage(messageFilePath string) (string, error) { } } - err = file.Close() - if err != nil { - fmt.Println("error while closing file:", err) - } - return output, nil } diff --git a/parseconfig.go b/parseconfig.go index c28d0cb..c9f2061 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -85,6 +85,7 @@ func parseConfig(configPath string) (configuration, error) { if err != nil { return output, fmt.Errorf("parseConfig: failed to open config file: %w", err) } + defer file.Close() scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) @@ -138,10 +139,6 @@ func parseConfig(configPath string) (configuration, error) { } } } - err = file.Close() - if err != nil { - fmt.Println("error closing file:", err) - } // Check if the username is a valid JID output.username, err = MarshalJID(output.username)