In this tutorial, you will learn what is data types and different types of data types such as int, float, string, etc are available in the Go programming language with examples.
Data types refer to the type of data a variable such as int
, float
, string
, etc can contain. This is also associated with the size of data a variable can store in the Go programming language. For example:
var X int = 25;
Here, X
is a variable of int
(integer) type and the size of the int
is 32 bit for 32 bit systems and 64 bit for 64 bit systems.
Go Data Types
Data types in the Go programming language can be broadly classified as follows:
- Basic Types
- Composite Types
The above two types can be further divided into the following sub-categories.
1) Basic Data Types
- Integers
- Floats
- String
- Complex Numbers
- Byte
- Rune
- Boolean
2) Composite Data Types
- Aggregate or Non-reference types
- Reference Types
- Interface Types
Integer Types
Integer data types are used to store integer data. They can be two types: signed and unsigned.
Data Type | Description |
int | Platform dependent. 32 bit or 64 bit |
int8 | Signed 8-bit integers (-128 to 127) |
int16 | Signed 16-bit integers (-32768 to 32767) |
int32 | Signed 32-bit integers (-2147483648 to 2147483647) |
int64 | Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) |
uint | Platform dependent. 32 bit or 64 bit |
uint8 | Unsigned 8-bit integers (0 to 255) |
uint16 | Unsigned 16-bit integers (0 to 65535) |
uint32 | Unsigned 32-bit integers (0 to 4294967295) |
uint64 | Unsigned 64-bit integers (0 to 18446744073709551615) |
Float Types
Float data type stores floating-point numbers i.e. numbers with a decimal point.
Two types of float data types are available in Go. They are:
Data Type | Description |
float32 | IEEE-754 32-bit floating-point numbers |
float64 | IEEE-754 64-bit floating-point numbers |
float64
is the default float type. When you initialize a variable with a decimal value and don’t specify the float type, the default type inferred will be float64
.