

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: The Error Interface
incomplete
2: Formatting Strings Review
incomplete
3: The Error Interface
incomplete
4: Errors Quiz
incomplete
5: The Errors Package
incomplete
6: Panic
incomplete
7: User Input
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Because errors are just interfaces, you can build your own custom types that implement the error interface. Here's an example of a userError struct that implements the error interface:
type userError struct {
name string
}
func (e userError) Error() string {
return fmt.Sprintf("%v has a problem with their account", e.name)
}
It can then be used as an error:
func sendSMS(msg, userName string) error {
if !canSendToUser(userName) {
return userError{name: userName}
}
...
}
Our users frequently try to run custom analytics queries on their message deliverability metrics, and end up writing a bad query that tries to divide a number by zero. It's become such a problem that we need to make a new type of error for division by zero.
Update the code so that the divideError type implements the error interface. Its Error() method should just return a string formatted in the following way:
cannot divide DIVIDEND by zero
Where DIVIDEND is the actual dividend of the divideError. Use the %v verb to format the dividend as a float.