# Golang : Calculate diameter, circumference, area, sphere surface and volume

Tags : golang physics circle diameter sphere-volume circumference

Let's travel back to high school for a while and learn some physics. We will learn how to calculate a circle's area, diameter, circumference, sphere surface and sphere volume the proper way.

I saw many programmers calculate the sphere volume the wrong way in Golang. While the result might appear to be correct mathematically, but wrong. A simple division by float or integer will make a big difference in the sphere volume result.

`4/3`

is not the same as `4/3.0`

. The code below will demonstrate the differences and as a programmer, it is our job to look out for a simple mistake such as this. You never know in future, some one's life might depend on your code.

Here you go!

```
package main
import (
"fmt"
"math"
)
var (
radius, diameter, circumference, area, sphereSurface, sphereVolume float64
)
func main() {
fmt.Println("Enter the radius of a circle (in meter): ")
_, err := fmt.Scanf("%f", &radius)
if err != nil {
fmt.Println(err)
}
fmt.Println("You have entered radius of : ", radius)
diameter = 2 * radius
circumference = 2 * math.Pi * radius
area = math.Pi * math.Pow(radius, 2)
sphereSurface = 4 * math.Pi * math.Pow(radius, 2)
// Go to be careful here, casting float64 over 4/3 is pointless
// because 4/3 is still 1
// The correct way is to do 4.0/3.0
sphereVolume = float64((4.0 / 3.0)) * (math.Pi * math.Pow(radius, 3))
// this will give wrong result
//sphereVolume = 4 / 3 * math.Pi * radius * radius * radius
fmt.Println("Diameter (m) : ", diameter)
fmt.Println("Circumference (m): ", circumference)
fmt.Println("Area (m2) : ", area)
fmt.Println("Sphere surface (m2) : ", sphereSurface)
fmt.Println("Sphere volume (m3) [correct way] : ", sphereVolume)
sphereVolume = 4 / 3 * math.Pi * radius * radius * radius // <--- WRONG!
fmt.Println("Sphere volume (m3) [wrong way] : ", sphereVolume)
// compare your calculation result with
// result from http://www.rkm.com.au/CALCULATORS/CALCULATOR-circle-sphere.html
// and http://www.calculatorsoup.com/calculators/geometry-solids/sphere.php
}
```

Output:

Enter the radius of a circle (in meter):

5

You have entered radius of : 5

Diameter (m) : 10

Circumference (m): 31.41592653589793

Area (m2) : 78.53981633974483

Sphere surface (m2) : 314.1592653589793

Sphere volume (m3) [correct way] : 523.5987755982987

Sphere volume (m3) [wrong way] : 392.69908169872417

**References:**

http://www.rkm.com.au/CALCULATORS/CALCULATOR-circle-sphere.html

#### See also : Golang : Find change in a combination of coins example

Tags : golang physics circle diameter sphere-volume circumference

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+1.8k Golang : Test input string for unicode example

+2.6k Golang : Flip coin example

+2.2k Android Studio : Checkbox for user to select options example

+2.4k Golang : Qt splash screen with delay example

+2.9k Golang : How to generate Code 39 barcode?

+4.5k Golang : interface - when and where to use examples

+2.6k Golang : Eroding and dilating image with OpenCV example

+18.1k Golang : Copy directory - including sub-directories and files

+5.1k Golang : How to detect a server/machine network interface capabilities?

+4.6k Golang : Timeout example

+4.7k Golang : Calculations using complex numbers example

+9.3k Golang : Randomly pick an item from a slice/array example