0
0
Goprogramming~15 mins

Multiple return values in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create variables for rectangle dimensions
Create two int variables called length and width and set them to 5 and 3 respectively.
Go
Hint

Use := to declare and assign variables in Go.

2
Create the rectangleMetrics function
Create a function called rectangleMetrics that takes two int parameters named length and width. It should return two int values.
Go
Hint

Calculate area as length * width and perimeter as 2 * (length + width). Return both values.

3
Call the function and store results
Call the function rectangleMetrics with length and width variables. Store the returned values in two variables called area and perimeter.
Go
Hint

Use multiple assignment to receive both returned values.

4
Print the area and perimeter
Print the values of area and perimeter with the exact text: Area: followed by the area value, and Perimeter: followed by the perimeter value.
Go
Hint

Use fmt.Println to print each value with its label.