For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:
A sphere is a three dimensional shape. The volume of sphere is the amount of air that a sphere can be held inside it. The formula for calculating the volume of a sphere with radius ‘r’ is given by
Sphere Volume = 4/3 πr³
Surface area of a sphere is defined as the total area covered its outer surface. The formula for calculating the surface area of a sphere with radius ‘r’ is given by
Sphere Surface Area = 4πr²
Where π value is 3.14 and r is the radius of the sphere
Here we are showing how to find the volume and surface area of a sphere in the Go language. Here variable spRa holds the radius of the sphere. Other variables spAr, spVol is used as the result variable of surface area and volume. Use the mathematical functions for the calculation. Volume is found by using 4πr³ and surface area by using 4πr². Finally print the results. Given below are the steps which are used in the Go program.
STEP 1: Import the package fmt
STEP 2: Start function main()
STEP 3: Declare the variable spRa, spAr, spVol
STEP 4: Read the radius of the sphere spRa
STEP 5: Calculate the surface area by using 4πr²
STEP 6: Calculate the volume by using 4/3πr³
STEP 7:Print the spAr and spVol using fmt.Println()
package main
import "fmt"
func main() {
var spRa, spAr, spVol
fmt.Print("Enter the radius of Sphere = ")
fmt.Scanln(&spRa;)
spAr = 4 * 3.14 * spRa * spRa
spVol = (4.0/3) * 3.14 * spRa * spRa * spRa
fmt.Println("The surface area of a sphere = ", spAr)
fmt.Println("The volume of a sphere = ", spVol)
}
Enter the radius of Sphere = 8 The surface area of a sphere = 803.84 The volume of a sphere = 2143.573333