Go Program to Calculate Area and Perimeter of Different Shapes

This page contains a Go program that calculates the area and perimeter of different shapes: circle, rectangle, and triangle.

Program Explanation

The Go program follows these steps:

  1. Define the main package and import necessary packages: Use package main and import the fmt and math packages.
  2. Define functions for each shape: Create separate functions to calculate the area and perimeter of a circle, rectangle, and triangle.
  3. Main Function: The main function is the entry point of the program where the functions are called and results are printed.

Go Program Code

package main

import (
    "fmt"
    "math"
)

/**
 * Function to calculate the area and perimeter of a circle.
 * @param radius the radius of the circle
 * @return the area and perimeter of the circle
 */
func circleAreaPerimeter(radius float64) (float64, float64) {
    area := math.Pi * radius * radius
    perimeter := 2 * math.Pi * radius
    return area, perimeter
}

/**
 * Function to calculate the area and perimeter of a rectangle.
 * @param length the length of the rectangle
 * @param width the width of the rectangle
 * @return the area and perimeter of the rectangle
 */
func rectangleAreaPerimeter(length, width float64) (float64, float64) {
    area := length * width
    perimeter := 2 * (length + width)
    return area, perimeter
}

/**
 * Function to calculate the area and perimeter of a triangle.
 * @param a the length of the first side
 * @param b the length of the second side
 * @param c the length of the third side
 * @return the area and perimeter of the triangle
 */
func triangleAreaPerimeter(a, b, c float64) (float64, float64) {
    perimeter := a + b + c
    s := perimeter / 2 // semi-perimeter
    area := math.Sqrt(s * (s - a) * (s - b) * (s - c)) // Heron's formula
    return area, perimeter
}

/**
 * Main function which is the entry point of the program.
 */
func main() {
    // Calculate and print area and perimeter for a circle
    radius := 5.0
    circleArea, circlePerimeter := circleAreaPerimeter(radius)
    fmt.Printf("Circle: Radius = %.2f, Area = %.2f, Perimeter = %.2f\n", radius, circleArea, circlePerimeter)

    // Calculate and print area and perimeter for a rectangle
    length := 10.0
    width := 5.0
    rectangleArea, rectanglePerimeter := rectangleAreaPerimeter(length, width)
    fmt.Printf("Rectangle: Length = %.2f, Width = %.2f, Area = %.2f, Perimeter = %.2f\n", length, width, rectangleArea, rectanglePerimeter)

    // Calculate and print area and perimeter for a triangle
    a, b, c := 3.0, 4.0, 5.0
    triangleArea, trianglePerimeter := triangleAreaPerimeter(a, b, c)
    fmt.Printf("Triangle: Sides = %.2f, %.2f, %.2f, Area = %.2f, Perimeter = %.2f\n", a, b, c, triangleArea, trianglePerimeter)
}

Program Details

The program contains the following:

  • circleAreaPerimeter Function: Calculates and returns the area and perimeter of a circle given its radius.
  • rectangleAreaPerimeter Function: Calculates and returns the area and perimeter of a rectangle given its length and width.
  • triangleAreaPerimeter Function: Calculates and returns the area and perimeter of a triangle given the lengths of its sides using Heron’s formula.
  • main Function: The entry point of the program where the functions are called with sample values and the results are printed.

Running the Program

To run the program:

  1. Save the code in a file named main.go.
  2. Open a terminal and navigate to the directory where the file is saved.
  3. Run the program using the command: go run main.go.

 

Explanation of the Go Program:

  1. Package and Imports: The program starts with the main package declaration and imports the necessary fmt and math packages.
  2. Functions for Each Shape:
    • circleAreaPerimeter: Calculates the area and perimeter of a circle given its radius.
    • rectangleAreaPerimeter: Calculates the area and perimeter of a rectangle given its length and width.
    • triangleAreaPerimeter: Calculates the area and perimeter of a triangle given the lengths of its sides using Heron’s formula.
  3. main Function:
    • Defines sample values for a circle, rectangle, and triangle.
    • Calls the respective functions to calculate the area and perimeter.
    • Prints the results to the console using fmt.Printf.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)