Created
March 8, 2022 16:20
-
-
Save xvbnm48/c7f0a8b86077d5f4e68737fd68795315 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "errors" | |
| "fmt" | |
| "strings" | |
| ) | |
| func validate(input string) (bool, error) { | |
| if strings.TrimSpace(input) == "" { | |
| return false, errors.New("cannot be empty") | |
| } | |
| return true, nil | |
| } | |
| func catch() { | |
| if r := recover(); r != nil { | |
| fmt.Println("Error occured", r) | |
| } else { | |
| fmt.Println("Application running perfectly") | |
| } | |
| } | |
| func main() { | |
| defer catch() | |
| var name string | |
| fmt.Print("Type your name: ") | |
| fmt.Scanln(&name) | |
| if valid, err := validate(name); valid { | |
| fmt.Println("halo", name) | |
| } else { | |
| panic(err.Error()) | |
| fmt.Println("end") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment