Skip to content

Instantly share code, notes, and snippets.

@xvbnm48
Created March 8, 2022 16:20
Show Gist options
  • Select an option

  • Save xvbnm48/c7f0a8b86077d5f4e68737fd68795315 to your computer and use it in GitHub Desktop.

Select an option

Save xvbnm48/c7f0a8b86077d5f4e68737fd68795315 to your computer and use it in GitHub Desktop.
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