Built-in, predeclared types
Basic types
Numeric types
Go has 12 numeric types group into three categories.
Integers
Type name | Value range |
---|---|
int8 | –128 to 127 |
int16 | –32768 to 32767 |
int32 | –2147483648 to 2147483647 |
int64 | –9223372036854775808 to 9223372036854775807 |
uint8 | 0 to 255 |
uint16 | 0 to 65535 |
uint32 | 0 to 4294967295 |
uint64 | 0 to 18446744073709551615 |
[[Go zero values | Zero value]] is 0 for all of them. |
Integer literals defaults to int
Special integers
byte
is an alias for uint8
. It’s more common to use byte
instead of uint8
.
int
is special in a way that on 32-bit CPU int
is a 32-signed integer like int32
, and on 64-bit CPUs int
is an int64
.
uint
is the same as int
, but it’s unsigned.
q Add more to that:
rune
and uintptr
Which integer to use
- When working on a binary file format or network protocol that dictates some integer type, use that.
- When working on a library, use generic types to support any integer type.
- In all other cases use
int
, unless you have a specific reason to use a sized or unsigned integer type.
Floating-point types
Type name | Largest absolute value | Smallest (nonzero) absolute value |
---|---|---|
float32 | 3.40282346638528859811704183484516925440e+38 | 1.401298464324817070923729583289916131280e-45 |
float64 | 1.797693134862315708145274237317043567981e+308 | 4.940656458412465441765687928682213723651e-324 |
Go uses the IEEE 754 specification, giving a large range and limited precision.
Floating-point literals have a float64
type by default.
Unless you need to be compatible with some format, use float64
. It’s also more precise than float32
.
Use floats in things like graphics, statistics and scientific operations. For everything else floats may not be a good fit because floating-point numbers have a huge range, but they store the nearest approximation, not every possible value in that range.
Operators
All operators can be used except %
.
Dividing nonzero float by 0 returns +Inf
or -Inf
depending of the sign of the number.
Dividing a float = 0 by 0 returns NaN
(Not a Number).
Go allows comparison with == and != on floats, but giving their inexact nature it may produce unexpected results. Instead define a maximum allowed variance and see if the difference between two floats is less than that. It’s called epsilon.
Complex types
Go has first-class support for complex numbers.
complex64
uses two float32
values to represent the real and imaginary part.
complex128
uses two float64
values.
Both are declared with built-in complex()
function.
- Untyped constants or literals for both function parameters will produce untyped complex literal that defaults to
complex128
. - Two
float32
=complex64
. - One is
float32
and another is untyped or can fit intofloat32
=complex64
. - Otherwise =
complex128
.
To extract the real and imaginary parts of the complex number use real()
and imag()
built-in functions respectively.
math/cmplx
package has additional functions to manipulate complex numbers.
Runes
rune
is an alias for uint8
.
Composite types
Array
Slice
String
Map
Structs
Basic types
Type conversion
Go doesn’t allow automatic type promotion, so you need to explicitly specify type conversion when dealing with integers or floats of different sizes.
Since all conversions are explicit, you can’t treat another Go type as a boolean. Because of that you need to explicitly check for zero value of a type, e.g. s == ""
or x == 0
.