errors library explained

This commit is contained in:
Mert Gör ☭ 2024-03-15 16:22:30 +03:00
parent c7da2836f7
commit 543286da57
Signed by: hwpplayer1
GPG Key ID: 03E547D043AB6C8F
3 changed files with 34 additions and 1 deletions

32
error_handling/demo2.go Normal file
View 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
View File

@ -1,3 +1,3 @@
module golesson
go 1.22.0
go 1.22.1

View File

@ -123,4 +123,5 @@ func main() {
error_handling.Demo1()
interfaces.Demo3()
error_handling.Demo2()
}