Learn Go← Dashboard

Numbers, booleans, strings, bytes, runes, and type conversion.

Numbers and Booleans

Go has a small, predictable set of built-in numeric types.

Integer types

| Type | Size | Range | | -------- | ------------------- | ------------------------------------------- | | int8 | 8 bits | −128…127 | | int16 | 16 bits | −32 768…32 767 | | int32 | 32 bits | ~ ±2.1 ×10⁹ | | int64 | 64 bits | ~ ±9.2 ×10¹⁸ | | int | 32 or 64 (platform) | "natural" int; use this 99% of the time | | uintN | unsigned variants | | | byte | alias for uint8 | | | rune | alias for int32 | a single Unicode code point |

Default rule: use int unless you have a reason. For sizes and indices, you already are using intlen(x) and cap(x) return int.

Floats and complex

var f32 float32 = 3.14
var f64 float64 = 3.14159265358979 // default literal type
var c   complex128 = 2 + 3i        // yes, complex numbers are built-in

Numeric literals

Go has a nice set of literal forms:

0xff           // hex
0o777          // octal
0b1010_1100    // binary, underscores allowed anywhere
1_000_000      // underscore as visual separator
1.5e3          // 1500.0
go playground
Loading...

Operator quick-reference

+ - * / %                arithmetic
== != < <= > >=          comparison
&& || !                  boolean
& | ^ << >> &^           bitwise (and / or / xor / shift / and-not)
+= -= *= ...             compound assignment
++ --                    statements only — `i++` is NOT an expression!

i++ and i-- are statements in Go — you can write i++ but not x = i++.

Comparing floats

== works on floats but is dangerous because of rounding:

0.1 + 0.2 == 0.3 // false — 0.1+0.2 is 0.30000000000000004

Compare with a tolerance instead: math.Abs(a-b) < 1e-9.

Boolean type

var ok bool = true
done := false

Booleans aren't integers in Go. You cannot do if 1 — only real bool values pass through if, for, etc. Logical operators && and || short-circuit.

go playground
Loading...
What does `5 / 2` evaluate to in Go?