Organized code and practiced error handling
Just found out nil is the equivalent of NULL
This commit is contained in:
parent
e928ff3728
commit
47d305243a
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
module between-1-10-checker
|
||||||
|
|
||||||
|
go 1.21.6
|
|
@ -1,7 +1,5 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
var num float64
|
var num float64
|
||||||
var name string
|
var name string
|
||||||
|
|
||||||
|
@ -13,23 +11,7 @@ func main() {
|
||||||
compare(num)
|
compare(num)
|
||||||
}
|
}
|
||||||
|
|
||||||
func startup(number float64) {
|
// You know? This is pretty fun, might be useless to organize so little code like this but it's nice seeing
|
||||||
fmt.Printf("Alright %s, give me a number, I will tell you if it's between 1 and 10\n", name)
|
// how clean everything afterwards
|
||||||
fmt.Printf("Number: ")
|
|
||||||
fmt.Scan(&num)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func nameget() {
|
// Until I learn proper error handling and OS/Network interactions I'll be having fun like this
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue