The Go Programming Language Specification
Integer literals
An integer literal is a sequence of digits representing an integer
constant.
Floating-point literals
A floating-point literal is a decimal representation of a
floating-point constant. It has an integer part, a decimal point, a
fractional part, and an exponent part. The integer and fractional part
comprise decimal digits; the exponent part is an e or E followed by an
optionally signed decimal exponent. One of the integer part or the
fractional part may be elided; one of the decimal point or the
exponent may be elided.
Arithmetic operators
For two integer values x and y, the integer quotient q = x / y and
remainder r = x % y satisfy the following relationships:
x = q*y + r and |r| < |y|
with x / y truncated towards zero.
You wrote, using integer literals and arithmetic (x / y truncates towards zero):
package main
import (
"fmt"
"strconv"
)
func main() {
var num float64
num = 5 / 3 // float64(int(5)/int(3))
fmt.Printf("%v
", num)
numString := strconv.FormatFloat(num, 'f', -1, 64)
fmt.Println(numString)
}
Playground: https://play.golang.org/p/PBqSbpHvuSL
Output:
1
1
You should write, using floating-point literals and arithmetic:
package main
import (
"fmt"
"strconv"
)
func main() {
var num float64
num = 5.0 / 3.0 // float64(float64(5.0) / float64 (3.0))
fmt.Printf("%v
", num)
numString := strconv.FormatFloat(num, 'f', -1, 64)
fmt.Println(numString)
}
Playground: https://play.golang.org/p/Hp1nac358HK
Output:
1.6666666666666667
1.6666666666666667