A function can return one or more values to the caller:
func AddAndMultiply(a, b int) (int, int) {
return a+b, a*b
}
The second return value can also be the error var :
import errors
func Divide(dividend, divisor int) (int, error) {
if divisor == 0 {
return 0, errors.New("Division by zero forbidden")
}
return dividend / divisor, nil
}
Two important things must be noted:
return
statement must provide a value for all declared return values.