How to calculate the area and perimeter of common geometric shapes (circle, rectangle, triangle) in Go?

To calculate the area and perimeter of common geometric shapes in Go, you can use the following formulas and implement them in your Go code:

  1. Circle:
    • Area: A = π * r^2 (where r is the radius of the circle)
    • Perimeter: P = 2 * π * r
package main import ( "fmt" "math" ) func main() { radius := 5.0 area := math.Pi * math.Pow(radius, 2) perimeter := 2 * math.Pi * radius fmt.Printf("Circle:\nArea: %f\nPerimeter: %f\n", area, perimeter) }
  1. Rectangle:
    • Area: A = length * width
    • Perimeter: P = 2 * (length + width)
package main import "fmt" func main() { length := 5.0 width := 3.0 area := length * width perimeter := 2 * (length + width) fmt.Printf("Rectangle:\nArea: %f\nPerimeter: %f\n", area, perimeter) }
  1. Triangle:
    • Area: A = (base * height) / 2
    • Perimeter: P = side1 + side2 + side3
package main import "fmt" func main() { base := 4.0 height := 3.0 side1 := 5.0 side2 := 6.0 side3 := 7.0 area := (base * height) / 2 perimeter := side1 + side2 + side3 fmt.Printf("Triangle:\nArea: %f\nPerimeter: %f\n", area, perimeter) }

You can customize the input values for each shape as per your requirement. The respective formulas will calculate the area and perimeter accordingly.