mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-18 19:21:17 +00:00
first
This commit is contained in:
41
course/6-errors/exercises/3-custom_errors/code.go
Normal file
41
course/6-errors/exercises/3-custom_errors/code.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type divideError struct {
|
||||
dividend float64
|
||||
}
|
||||
|
||||
// ?
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func divide(dividend, divisor float64) (float64, error) {
|
||||
if divisor == 0 {
|
||||
// We convert the `divideError` struct to an `error` type by returning it
|
||||
// as an error. As an error type, when it's printed its default value
|
||||
// will be the result of the Error() method
|
||||
return 0, divideError{dividend: dividend}
|
||||
}
|
||||
return dividend / divisor, nil
|
||||
}
|
||||
|
||||
func test(dividend, divisor float64) {
|
||||
defer fmt.Println("====================================")
|
||||
fmt.Printf("Dividing %.2f by %.2f ...\n", dividend, divisor)
|
||||
quotient, err := divide(dividend, divisor)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Quotient: %.2f\n", quotient)
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10, 0)
|
||||
test(10, 2)
|
||||
test(15, 30)
|
||||
test(6, 3)
|
||||
}
|
||||
Reference in New Issue
Block a user