Organized code and practiced error handling

Just found out nil is the equivalent of NULL
This commit is contained in:
raul 2024-01-22 14:23:12 +01:00
parent e928ff3728
commit 47d305243a
5 changed files with 47 additions and 21 deletions

View File

@ -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)
}
}

View File

@ -0,0 +1,3 @@
module between-1-10-checker
go 1.21.6

View File

@ -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

View File

@ -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)
}
}

View File

@ -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
}