Errors
In Go it’s idiomatic to communicate errors via an explicit, separate return value. Go’s approach makes it easy to see which functions return errors and to handle them using the same language constructs employed for any other, non-error tasks.
By convention, errors are the last return value and have type error, a built-in interface.
errors.New constructs a basic error value with the given error message.
func f1(arg int) (int, error) {
if arg == 42 {
return -1, errors.New("can't work with 42")
}
return arg + 3, nil
}