How to Use Named Return Values in Go

Named return values in Go allow you to declare return variable names in the function signature for automatic return and cleaner code.

Named return values in Go are declared in the function signature and automatically returned if the function exits without an explicit return statement. You can assign values to them anywhere in the function body, and they act as local variables that are returned by default.

func divide(a, b float64) (quotient, remainder float64) {
	quotient = a / b
	remainder = a % b
	// Returns (quotient, remainder) implicitly
}

If you need to return early or change the values, use the return keyword with or without arguments to send the current values of the named returns back to the caller.