From 47d305243ac6b8cb748b2d00095b2f733092b68e Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 22 Jan 2024 14:23:12 +0100 Subject: [PATCH] Organized code and practiced error handling Just found out nil is the equivalent of NULL --- golangr/between-1-10-checker/compare.go | 11 +++++++++++ golangr/between-1-10-checker/go.mod | 3 +++ golangr/between-1-10-checker/main.go | 24 +++--------------------- golangr/between-1-10-checker/nameget.go | 20 ++++++++++++++++++++ golangr/between-1-10-checker/startup.go | 10 ++++++++++ 5 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 golangr/between-1-10-checker/compare.go create mode 100644 golangr/between-1-10-checker/go.mod create mode 100644 golangr/between-1-10-checker/nameget.go create mode 100644 golangr/between-1-10-checker/startup.go diff --git a/golangr/between-1-10-checker/compare.go b/golangr/between-1-10-checker/compare.go new file mode 100644 index 0000000..24ffa3b --- /dev/null +++ b/golangr/between-1-10-checker/compare.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func compare(number float64) { + if number >= 1 && number <= 10 { + fmt.Printf("%v is between 1 and 10\n", number) + } else { + fmt.Printf("%v isn't between 1 and 10\n", number) + } +} diff --git a/golangr/between-1-10-checker/go.mod b/golangr/between-1-10-checker/go.mod new file mode 100644 index 0000000..60bb09e --- /dev/null +++ b/golangr/between-1-10-checker/go.mod @@ -0,0 +1,3 @@ +module between-1-10-checker + +go 1.21.6 diff --git a/golangr/between-1-10-checker/main.go b/golangr/between-1-10-checker/main.go index d125517..ac0de74 100644 --- a/golangr/between-1-10-checker/main.go +++ b/golangr/between-1-10-checker/main.go @@ -1,7 +1,5 @@ package main -import "fmt" - var num float64 var name string @@ -13,23 +11,7 @@ func main() { compare(num) } -func startup(number float64) { - fmt.Printf("Alright %s, give me a number, I will tell you if it's between 1 and 10\n", name) - fmt.Printf("Number: ") - fmt.Scan(&num) - return -} +// You know? This is pretty fun, might be useless to organize so little code like this but it's nice seeing +// how clean everything afterwards -func nameget() { - fmt.Println("Welcome to this simple program, may I know your name?\n(Please don't use spaces, I still haven't figured out how to parse them all together into a string)") - fmt.Printf("Name: ") - fmt.Scanln(&name) -} - -func compare(number float64) { - if number >= 1 && number <= 10 { - fmt.Printf("The number is between 1 and 10\n") - } else { - fmt.Printf("The number isn't between 1 and 10\n") - } -} +// Until I learn proper error handling and OS/Network interactions I'll be having fun like this diff --git a/golangr/between-1-10-checker/nameget.go b/golangr/between-1-10-checker/nameget.go new file mode 100644 index 0000000..0c2e223 --- /dev/null +++ b/golangr/between-1-10-checker/nameget.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "log" +) + +func nameget() { + fmt.Println("Welcome to this simple program, may I know your name?\n(Please don't use spaces, I still haven't figured out how to parse them all together into a string)") + fmt.Printf("Name: ") + + // It has come to my attention that scan actually spits out two values, default and err + _, err := fmt.Scanln(&name) + + // nil is literally the equivalent of null, we're just checking here if err is not null, + // and if it isn't, it means an error has ocurred so we're just gonna panic and kill the program + if err != nil { + log.Fatal(err) + } +} diff --git a/golangr/between-1-10-checker/startup.go b/golangr/between-1-10-checker/startup.go new file mode 100644 index 0000000..444887b --- /dev/null +++ b/golangr/between-1-10-checker/startup.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func startup(number float64) { + fmt.Printf("Alright %s, give me a number, I will tell you if it's between 1 and 10\n", name) + fmt.Printf("Number: ") + fmt.Scan(&num) + return +}