1009 B
1009 B
Errors
Errors are handled using standard error type.
When neccessary (e.g. returning back to caller from another package) fmt.Errorf should be used.
General error syntax is:
$NAME $TYPE: %w
Where:
$NAMEis a type's name.$TYPEis a type as string.
Do not try to return dynamically formatted error. If error's text (or part of it) is static - put it into a variable, and if that static text appears more than once in different functions - put it into unexported package variable.
Errors can be set as exported variable only if it supposed to be checked (or actually checked) in another package.
Good example:
package a
import "errors"
var errA = errors.New("a")
func someFunc() error {
if err := otherFunc(); err != nil {
return fmt.Errorf("%w: someFunc: %w", errA, err)
}
return nil
}
Bad example:
package a
import errors
func someFunc() error {
if err := otherFunc(); err != nil {
return err
}
return nil
}