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.go349 chars20 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