find the volume of a cylinder in go

To find the volume of a cylinder in Go, we need to use the formula π * r^2 * h, where π is the mathematical constant pi, r is the radius of the cylinder, and h is the height of the cylinder.

We can write a function in Go that takes the radius and height of the cylinder as inputs, and returns its volume as output:

main.go
package main

import (
    "fmt"
    "math"
)

func cylinderVolume(radius float64, height float64) float64 {
    volume := math.Pi * math.Pow(radius, 2) * height
    return volume
}

func main() {
    // Example usage:
    radius := 3.0
    height := 4.0
    volume := cylinderVolume(radius, height)
    fmt.Println("Volume of cylinder:", volume)
}
349 chars
20 lines

In this code, we import the math package to use the Pi and Pow() functions. We define a cylinderVolume() function that takes two parameters radius and height, which are both of type float64. We compute the volume using the formula and return it as a float64.

In the main() function, we demonstrate how to use our cylinderVolume() function by calculating the volume of a cylinder with a radius of 3 and a height of 4. We print the result to the console.

gistlibby LogSnag