From a7ba6afcbf958beb24588936d6209bb20dcb053c Mon Sep 17 00:00:00 2001 From: Jon Bodner Date: Sat, 28 Nov 2020 23:56:55 -0500 Subject: [PATCH] improve error handling --- main.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 6004542..5342791 100755 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "os" "github.com/learning-go-book/formatter" @@ -9,8 +10,18 @@ import ( ) func main() { - amount, _ := decimal.NewFromString(os.Args[1]) - percent, _ := decimal.NewFromString(os.Args[2]) + if len(os.Args) < 3 { + fmt.Println("Need two parameters: amount and percent") + os.Exit(1) + } + amount, err := decimal.NewFromString(os.Args[1]) + if err != nil { + log.Fatal(err) + } + percent, err := decimal.NewFromString(os.Args[2]) + if err != nil { + log.Fatal(err) + } percent = percent.Div(decimal.NewFromInt(100)) total := amount.Add(amount.Mul(percent)).Round(2) fmt.Println(formatter.Space(80, os.Args[1], os.Args[2], total.StringFixed(2)))