errors library explained
This commit is contained in:
parent
c7da2836f7
commit
543286da57
3 changed files with 34 additions and 1 deletions
32
error_handling/demo2.go
Normal file
32
error_handling/demo2.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package error_handling
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func GuessIt(guess int) (string, error){
|
||||||
|
|
||||||
|
number_in_mind := 50
|
||||||
|
|
||||||
|
if guess < 1 || guess > 100{
|
||||||
|
return "", errors.New("Type a number between 1 and 100 ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if guess > number_in_mind{
|
||||||
|
return "Type a small number", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if guess < number_in_mind{
|
||||||
|
return "Type a bigger number", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "You did it", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Demo2() {
|
||||||
|
message, error:= GuessIt(101)
|
||||||
|
fmt.Println(message)
|
||||||
|
fmt.Println(error)
|
||||||
|
}
|
2
go.mod
2
go.mod
|
@ -1,3 +1,3 @@
|
||||||
module golesson
|
module golesson
|
||||||
|
|
||||||
go 1.22.0
|
go 1.22.1
|
||||||
|
|
1
main.go
1
main.go
|
@ -123,4 +123,5 @@ func main() {
|
||||||
|
|
||||||
error_handling.Demo1()
|
error_handling.Demo1()
|
||||||
interfaces.Demo3()
|
interfaces.Demo3()
|
||||||
|
error_handling.Demo2()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue