find the volume of a sphere in go

To find the volume of a sphere in Go, we will need to use the math package. The formula to find the volume of a sphere is (4/3) * pi * r^3, where r is the radius of the sphere.

Here's the code to find the volume of a sphere in Go:

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    var radius float64 = 4.5

    volume := (4.0/3.0) * math.Pi * math.Pow(radius, 3)

    fmt.Printf("Volume of sphere with radius %.2f is %.2f\n", radius, volume)
}
228 chars
15 lines

In this code, we first import the math package, which includes mathematical constants and functions.

We then define the radius of the sphere as a float64 variable. We calculate the volume using the formula, and store the result in the volume variable.

Finally, we print out the result using fmt.Printf(), which formats the string with the values of radius and volume.

gistlibby LogSnag