Named return values
📖 Scenario: Imagine you are writing a small program to calculate the area and perimeter of a rectangle. You want to create a function that returns both values clearly and simply.
🎯 Goal: Build a Go function that uses named return values to return the area and perimeter of a rectangle given its width and height.
📋 What You'll Learn
Create a function called
rectangleMetrics that takes two float64 parameters: width and height.Use named return values
area and perimeter of type float64 in the function signature.Calculate the area as
width * height and perimeter as 2 * (width + height) inside the function.Return the named values without explicitly listing them in the return statement.
Print the returned
area and perimeter values in the main function.💡 Why This Matters
🌍 Real World
Named return values are useful in real-world Go programs where functions return multiple results, like calculations or data processing.
💼 Career
Understanding named return values helps you write clean, readable Go code, a valuable skill for backend development and system programming jobs.
Progress0 / 4 steps