Multiple return values in Go
📖 Scenario: Imagine you are writing a small program to calculate the area and perimeter of a rectangle. You want a function that can give you both results at the same time.
🎯 Goal: Create a Go program with a function that returns two values: the area and the perimeter of a rectangle. Then print these values.
📋 What You'll Learn
Create a function called
rectangleMetrics that takes two int parameters: length and width.The function
rectangleMetrics must return two int values: the area and the perimeter.Call the function with
length = 5 and width = 3.Print the returned area and perimeter values with clear labels.
💡 Why This Matters
🌍 Real World
Functions that return multiple values are common in Go for returning results and errors or multiple related results together.
💼 Career
Understanding multiple return values is important for writing clean, efficient Go code in real-world applications like web servers, tools, and data processing.
Progress0 / 4 steps