Numbers & Math
7 min · Go
Do math with variables and the basic operators.
Enemy 1 of 5
Doing arithmetic
The familiar operators + - * / work in Go exactly as you'd expect, following standard order of operations. When both values involved are whole numbers (ints, inferred automatically from literals like 5 and 3), the result is also a whole number.
Just like in C, C++, and C#, dividing two ints in Go truncates any decimal remainder rather than rounding — 7 / 2 gives you 3, not 3.5. To get a precise fractional answer, at least one value needs to be a floating-point type, written float64 in Go.
You can pass an expression like price * quantity straight into fmt.Println without storing it in a variable first — Go evaluates the expression and prints the resulting value.
Loading editor...