find the area of a square in go

To find the area of a square in Go, you need to multiply the length of one side by itself.

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    var side float64 = 5
    area := math.Pow(side, 2)
    fmt.Printf("Area of the square is: %.2f", area)
}
170 chars
13 lines

In this example, we define the length of one side of the square as side and calculate the area by multiplying it by itself using the math.Pow() function. The result is then printed to the console using fmt.Printf().

Note that we use a float64 type for side and area variables to handle decimal values. Additionally, we use %.2f in fmt.Printf() to format the result up to two decimal places.

gistlibby LogSnag