sphere volume formula
Calculating the Volume of a Sphere in Go
To calculate the volume of a sphere in Go, you can use the following formula:
V = (4/3) π r^3
Where: - V is the volume of the sphere - π (pi) is a mathematical constant approximately equal to 3.14159 - r is the radius of the sphere
Here's an example code snippet that calculates the volume of a sphere in Go:
package main
import (
"fmt"
"math"
)
func main() {
radius := 5.0
volume := (4.0 / 3.0) math.Pi math.Pow(radius, 3)
fmt.Printf("The volume of the sphere with radius %.2f is %.2f\n", radius, volume)
}
In this code, we import the fmt
package for printing the result and the math
package for accessing the mathematical constant Pi
and the Pow
function for exponentiation. We then define the radius of the sphere as 5.0
and calculate the volume using the formula. Finally, we use fmt.Printf
to print the result.
When you run this code, it will output:
The volume of the sphere with radius 5.00 is 523.60
This means that a sphere with a radius of 5 units has a volume of 523.60 cubic units.
I hope this helps! Let me know if you have any further questions.