Complete the code to declare a function with named return values.
func greet() (message string) {
message = "Hello, Go!"
return [1]
}The function uses a named return value message. Returning message returns the named value.
Complete the code to use named return values and omit the return expression.
func add(a, b int) (sum int) {
sum = a + b
[1]
}When using named return values, a bare return returns the current values of those variables.
Fix the error in the function by completing the return statement correctly.
func divide(a, b float64) (result float64, err error) {
if b == 0 {
err = fmt.Errorf("division by zero")
[1]
}
result = a / b
return
}When division by zero occurs, result should be zero and err should hold the error. So return 0, err.
Fill both blanks to complete the function with named return values and a bare return.
func multiply(a, b int) (product int, err error) {
if a < 0 || b < 0 {
err = fmt.Errorf("negative input")
[1]
}
product = a * b
[2]
}If inputs are negative, return zero product and the error. Otherwise, assign product and use a bare return to return named values.
Fill all three blanks to complete the function using named return values and proper error handling.
func subtract(a, b int) (result int, err error) {
if a < b {
err = fmt.Errorf("a is less than b")
[1]
}
result = a - b
[2]
[3]
}When a < b, return zero and error. Otherwise, assign result and use bare return twice (the second bare return is redundant but included for the exercise).