From 75ff09e21478b7456445d58d2e45309508bd8f5f Mon Sep 17 00:00:00 2001 From: Mikhail Samin Date: Mon, 1 Feb 2021 22:25:26 +0300 Subject: [PATCH] Fixed a goroutine leak --- examples/scanFiles/main.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/examples/scanFiles/main.go b/examples/scanFiles/main.go index a33d5b8..8c90a4b 100644 --- a/examples/scanFiles/main.go +++ b/examples/scanFiles/main.go @@ -121,6 +121,7 @@ func RecognizeMultipleFiles(dir string, client *audd.Client, setID3, short bool, cover = "https://i3.ytimg.com/vi/"+strings.ReplaceAll(result[i].SongLink, "https://youtu.be/", "")+"/maxresdefault.jpg" } response, err := http.Get(cover) + defer closeBody(response) if err != nil { fmt.Println("Error: can't get the cover for", fileName, err) } else { @@ -198,6 +199,16 @@ func getCurrentDir() string { return filepath.Dir(currentFile) } +func closeBody(resp *http.Response) { + if resp == nil { + return + } + if resp.Body == nil { + return + } + _ = resp.Body.Close() +} + func main() { dir := flag.String("dir", getCurrentDir(), "The directory with the files") apiToken := flag.String("api_token", "test", "AudD API token")