|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +/* -------------------------------------------------------------------------- */ |
| 9 | +/* ERROR (CLASS) */ |
| 10 | +/* -------------------------------------------------------------------------- */ |
| 11 | + |
| 12 | +type Error struct { |
| 13 | + errorMessage string |
| 14 | + errorSeverity string |
| 15 | +} |
| 16 | + |
| 17 | +func (error *Error) Error() string { |
| 18 | + var errorSeverity string = fmt.Sprintf("Error severity: %s", error.errorSeverity) |
| 19 | + var errorMessage string = fmt.Sprintf("\nError message: %s", error.errorMessage) |
| 20 | + return errorSeverity + errorMessage |
| 21 | +} |
| 22 | + |
| 23 | +/* -------------------------------------------------------------------------- */ |
| 24 | +/* FUNCTIONS */ |
| 25 | +/* -------------------------------------------------------------------------- */ |
| 26 | + |
| 27 | +func divide(a int, b int) (int, error) { |
| 28 | + if b == 0 { |
| 29 | + var errorMessage string = fmt.Sprintf("Can't divide %d by %d value!", a, b) |
| 30 | + return 0, errors.New(errorMessage) |
| 31 | + } |
| 32 | + |
| 33 | + return a / b, nil |
| 34 | +} |
| 35 | + |
| 36 | +func at[T any](array *[]T, index int) (T, error) { |
| 37 | + var arrayLength int = len(*array) |
| 38 | + if arrayLength <= index { |
| 39 | + var emptyValue T |
| 40 | + var errorMessage string = fmt.Sprintf("Index out of range! It must be less than %d.", index) |
| 41 | + return emptyValue, errors.New(errorMessage) |
| 42 | + } |
| 43 | + |
| 44 | + return (*array)[index], nil |
| 45 | +} |
| 46 | + |
| 47 | +func fn(a int, b int) (int, error) { |
| 48 | + if a == 0 { |
| 49 | + return 0, errors.New("First parameter mustn't be zero!") |
| 50 | + } |
| 51 | + |
| 52 | + if b == 0 { |
| 53 | + return 0, errors.New("Second parameter mustn't be zero!") |
| 54 | + } |
| 55 | + |
| 56 | + if a < 0 || b < 0 { |
| 57 | + return 0, &Error{errorMessage: "First parameter and second parameter must be greater than zero!", errorSeverity: "high"} |
| 58 | + } |
| 59 | + |
| 60 | + return a + b, nil |
| 61 | +} |
| 62 | + |
| 63 | +/* -------------------------------------------------------------------------- */ |
| 64 | +/* MAIN */ |
| 65 | +/* -------------------------------------------------------------------------- */ |
| 66 | + |
| 67 | +func main() { |
| 68 | + /* |
| 69 | + Exceptions... |
| 70 | + */ |
| 71 | + |
| 72 | + fmt.Println("Exceptions...") |
| 73 | + |
| 74 | + division, divisionError := divide(10, 0) |
| 75 | + if divisionError != nil { |
| 76 | + fmt.Printf("\n%s\n", divisionError.Error()) |
| 77 | + } else { |
| 78 | + fmt.Printf("\n%d / %d --> %d\n", 10, 0, division) |
| 79 | + } |
| 80 | + |
| 81 | + var array []string = []string{"Hello", "World", "!"} |
| 82 | + element, elementError := at(&array, 3) |
| 83 | + if elementError != nil { |
| 84 | + fmt.Printf("\n%s\n", elementError.Error()) |
| 85 | + } else { |
| 86 | + fmt.Printf("\narray[3] --> %s\n", element) |
| 87 | + } |
| 88 | + |
| 89 | + fmt.Println("\n# ---------------------------------------------------------------------------------- #\n") |
| 90 | + |
| 91 | + /* |
| 92 | + Additional challenge... |
| 93 | + */ |
| 94 | + |
| 95 | + fmt.Println("Additional challenge...") |
| 96 | + |
| 97 | + // Custom error |
| 98 | + result01, result01Error := fn(-2, -2) |
| 99 | + if result01Error != nil { |
| 100 | + fmt.Printf("\n%s\n", result01Error.Error()) |
| 101 | + } else { |
| 102 | + fmt.Printf("\nFirst result --> %d\n", result01) |
| 103 | + } |
| 104 | + |
| 105 | + // First parameter error |
| 106 | + result02, result02Error := fn(0, 5) |
| 107 | + if result02Error != nil { |
| 108 | + fmt.Printf("\n%s\n", result02Error.Error()) |
| 109 | + } else { |
| 110 | + fmt.Printf("\nSecond result --> %d\n", result02) |
| 111 | + } |
| 112 | + |
| 113 | + // No error |
| 114 | + result03, result03Error := fn(5, 10) |
| 115 | + if result03Error != nil { |
| 116 | + fmt.Printf("\n%s\n", result03Error.Error()) |
| 117 | + } else { |
| 118 | + fmt.Printf("\nThird result --> %d\n", result03) |
| 119 | + } |
| 120 | + |
| 121 | + // Second parameter error |
| 122 | + result04, result04Error := fn(8, 0) |
| 123 | + if result04Error != nil { |
| 124 | + fmt.Printf("\n%s\n", result04Error.Error()) |
| 125 | + } else { |
| 126 | + fmt.Printf("\nFourth result --> %d\n", result04) |
| 127 | + } |
| 128 | + |
| 129 | + fmt.Println("\nAdditional challenge finished!") |
| 130 | +} |
0 commit comments