# How to find the Perimeter of a Rectangle in Golang?

In this tutorial, we are going to see the Golang program to find the perimeter of a rectangle. Perimeter is the total length of the boundary of any closed figure

## Formula

Perimeter of rectangle - 2 * ( L + B )
L - Length of a rectangle
B - Breadth of a rectangle


For example, the length of a rectangle is 100 cm and the breadth is 50 cm so the Perimeter of a rectangle is −

Perimeter = 2 * (100 + 50) cm
= 2 * (150) cm
= 300 cm


## Finding the Perimeter of a circle within the function

### Algorithm

• STEP 1 − Declaring the variables for the length, breadth, and the parameter of the float64 data type.

• STEP 2 − Taking the input for the length and breadth from the user.

• STEP 3 − Finding the perimeter using the above formula within the function.

• STEP 4 − Printing the result.

#### Time Complexity

O(1) − The time complexity is constant because no matter what is the input the program will take sane time.

#### Space Complexity

O(1) − The variables are static in the program so the space complexity is also constant.

### Example 1

In this example, we are going to find the Perimeter of a rectangle within the function.

package main

// fmt package provides the function to print anything
import (
"fmt"
)
func main() {

// declaring the floating variables using the var keyword for
// storing the length and breadth also a variable to store Perimeter
fmt.Println("Program to find the perimeter of a rectangle.")

// initializing the length of a rectangle
length = 30

// initializing the breadth of a rectangle

// finding the Perimeter of a rectangle
Perimeter = 2.0 * (length + breadth)

// printing the result
fmt.Println("Length = ", length, "\nBreath =", breadth, "\nPreimeter of the Rectangle = ", Perimeter, "cm.")
fmt.Println("(Finding the Perimeter of a circle within the function)")
}


### Output

Program to find the perimeter of a rectangle.
Length = 30
Breath = 10.4
Preimeter of the Rectangle = 80.8 cm.
(Finding the Perimeter of a circle within the function)


### Description of code

• var length, breadth, Perimeter float64 − In this line, we are declaring the length, breadth, and perimeter that we are going to use later. As the length, breadth, or perimeter can be in decimal so we have used the float data type.

• Perimeter = 2.0 * (length + breadth) − In this line of code, we are applying the formula and finding the perimeter.

• fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", Perimeter, "cm.") − Printing the perimeter of a rectangle.

## Finding the perimeter of a circle in different function

### Algorithm

• STEP 1 − Declaring the variables for the length, breadth, and the parameter of float64 data type.

• STEP 2 − Taking the input for the length and breadth from the user.

• STEP 3 − Calling the function with the length and breadth as a parameter, and storing the perimeter the function is returning

• STEP 4 − Printing the result.

### Example 2

In this example, we are going to find the Perimeter of a rectangle by defining the separate function to find the perimeter

package main

// fmt package provides the function to print anything
import (
"fmt"
)
func perimeterOfRectangle(length, breadth float64) float64 {
// returning the perimeter of a rectangle using the formula
return 2 * (length + breadth)
}
func main() {

// declaring the floating variables using the var keyword for
// storing the length and breadth also a variable to store parameter
fmt.Println("Program to find the perimeter of a rectangle.")

// taking the length of a rectangle as input from the user
fmt.Print("Please enter the length of a rectangle:")
fmt.Scanln(&length)

// taking the breadth of a rectangle as input from the user

// calling the perimeter of rectangle function by passing the respective parameter
// and storing the result

// printing the result
fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is",    perimeter, "cm.")
fmt.Println("(Finding the perimeter of a circle in different function)")
}


### Output

Program to find the perimeter of a rectangle.
Please enter the length of a rectangle:20.5
The parameter of a rectangle whose length is 20.5 and the breadth is 10 is 61 cm.
(Finding the perimeter of a circle in different function)


### Description of code:

• var length, breadth, Perimeter float64 − In this line, we are declaring the length, breadth, and perimeter that we are going to use later. As the length, breadth, or perimeter can be in decimal so we have used the float data type.

• fmt.Scanln(&length) − Taking the input for the length from the user

• fmt.Scanln(&breadth) − Taking the input for the breadth from the user.

• perimeter = perimeterOfRectangle(length, breadth) − In this line of code, we are calling the function that is finding the perimeter of a rectangle.

• fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", Perimeter, "cm.") − Printing the perimeter of a rectangle.

## Conclusion

These are the two ways to find the perimeter of a rectangle in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.