From 0010b6c1fca2f267ed4956192b377a16490becec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Thu, 2 Sep 2021 15:44:02 +0200 Subject: [PATCH] fixed panic: runtime error: index out of range [0] with length 0 panic: runtime error: index out of range [0] with length 0 goroutine 1 [running]: github.com/mrahbar/my-bloody-hetzner-sb-notifier/writer.(*TableWriter).Print(0xc00011be20, {{0x31e, 0x0}, {0x0, 0x0, 0x0}}) /home/philipp/git/my-bloody-hetzner-sb-notifier/writer/table.go:25 +0x27a main.run({0x731f00, 0xc0000ae008}, {0x0, 0x4041800000000000, 0x40, 0x100, 0x1800, 0x1800, 0x2, 0xf, ...}, ...) /home/philipp/git/my-bloody-hetzner-sb-notifier/main.go:251 +0x335 main.main() /home/philipp/git/my-bloody-hetzner-sb-notifier/main.go:138 +0x1a5 If no server was found we crashed, because we tried to print out the 0 element of an array. --- writer/table.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/writer/table.go b/writer/table.go index 630800d..dbb77a0 100644 --- a/writer/table.go +++ b/writer/table.go @@ -20,9 +20,12 @@ func NewTableWriter(output io.Writer)*TableWriter { func (c *TableWriter) Print(deals hetzner.Deals) { fmt.Fprintf(c.tabWriter,"Got %d offers. Filtered offers: %d\n", deals.ResultStats.OriginalCount, deals.ResultStats.FilteredCount) - fmt.Fprintf(c.tabWriter, "%s\n", deals.Servers[0].Header()) - for _, server := range deals.Servers { - fmt.Fprintf(c.tabWriter, "%s\n", server.ToString()) + if deals.ResultStats.FilteredCount != { + fmt.Fprintf(c.tabWriter, "%s\n", deals.Servers[0].Header()) + for _, server := range deals.Servers { + fmt.Fprintf(c.tabWriter, "%s\n", server.ToString()) + } + c.tabWriter.Flush() } - c.tabWriter.Flush() + }