From 906a634c406a35e2b525b40a9fe4cddb92de00b2 Mon Sep 17 00:00:00 2001 From: David Verbeiren Date: Wed, 15 Dec 2021 13:13:37 +0000 Subject: [PATCH] wait for result to be printed before exiting Measurements are read from a channel and, in silent mode, the only output is printed after all measurements have been read. That is when the channel provided to fast.Measure() has been closed at the end of fast.Measure() and drained by the goroutine reading from the measurements. main() must wait for that goroutine that eventually prints the test result before exiting. Signed-off-by: David Verbeiren --- fast.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/fast.go b/fast.go index ed83a32..e5169dc 100644 --- a/fast.go +++ b/fast.go @@ -60,8 +60,8 @@ func main() { // measure KbpsChan := make(chan float64) - - go func() { + done := make(chan bool) + go func(done chan bool) { var value, units string for Kbps := range KbpsChan { value, units = format(Kbps, kb, mb, gb) @@ -78,7 +78,9 @@ func main() { } else { fmt.Printf("\r%c[2K -> %s\n", 27, status) } - }() + + done <- true + }(done) err = fastCom.Measure(urls, KbpsChan) ticker.Stop() @@ -87,6 +89,9 @@ func main() { os.Exit(1) } + // finish reading KbpsChan so that the result always gets printed + <- done + return }