Go Data Types Tutorial
In this Go tutorial we introduce the basic numeric data types, signed and unsigned integers and floats.
What are data types
When we create data containers, like variables, the computer has to know which type of data we’re storing.
The types of data we use will determine how much space the container takes up in memory. The size of these data types may change depending on a 32 or 64-bit operating system.
For example, if we want to store numbers, we would tell the compiler that the data container will only contain floating point numbers or whole integer numbers.
Data types in Go can be classified as the following:
- Boolean types. Predefined constants with only two values, true or false.
- Numeric types. Arithmetic types which represent intergers (whole numbers) or floating point numbers.
- String types. A sequence of individual characters that make up words or sentences.
- Derived types. These include arrays, structs, functions, slices, interfaces, unions, maps, channels and pointers.
In this tutorial lesson we will discuss the basic numeric types. The types that aren’t covered here will be discussed in their own sections later in the tutorial course.
Integer types
Ints are simply whole numbers within a certain range. Numbers are specified in ranges because it affects how much space they take up in memory.
The following int types are available in Go.
Unsigned Int | Range |
---|---|
uint8 | 0 to 255 |
uint16 | 0 to 65535 |
uint32 | 0 to 4294967295 |
uint64 | 0 to 18446744073709551615 |
The unsigned integers above may not contain numbers with a negative value.
Signed Int | Range |
---|---|
int8 | -128 to 127 |
int16 | -32768 to 32767 |
int32 | -2147483648 to 2147483647 |
int64 | -9223372036854775808 to 9223372036854775807 |
The signed integers above may contain numbers with negative values.
Floating point types
Floats are similar to integers except that they may contain numbers with floating point values. For example, 3.14.
The following float types are available in Go.
Type | Description |
---|---|
float32 | IEEE-754 32-bit floating-point numbers |
float64 | IEEE-754 64-bit floating-point numbers |
complex64 | Complex numbers with float32 real and imaginary parts |
complex128 | Complex numbers with float64 real and imaginary parts |
Boolean types
A bool can technically be considered a numeric type because its values of true and false represent the numeric values of 1 and 0 respectively.
Summary: Points to remember
- Data types specify the type of data that is being stored in memory by the program.
- Data types determine the amount of space taken up in system memory and may change depending on the operating system.
- The data types in Go are classified as boolean, numeric, string and derived types.