mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-21 19:16:23 +00:00
921 B
921 B
date | id | title |
---|---|---|
2020-09-09 | 5114b51a-da22-4c9c-a12c-c416aeb984b6 | Golang Errors |
Basics
Go supports Errors
import (
"errors"
"fmt"
)
func ThrowError(throwError bool) error {
if throwError {
return errors.New("This is an error")
}
fmt.Println("No error was thrown!")
return nil
}
func main() {
ThrowError(true)
ThrowError(false)
}
Wrappers
It appears to be good practice to make errors a const
. This example
uses the Error interface:
const (
ErrNotFound = DictionaryErr("could not find the word you were looking for")
ErrWordExists = DictionaryErr("cannot add word because it already exists")
)
type DictionaryErr string
func (e DictionaryErr) Error() string {
return string(e)
}
Handy Links
- Constant errors1