Go Data Types
In Go, built-in data types are categorized as below:
- Basic types: boolean, numeric, string
- Reference types: pointers, slices, maps, and channels
- Aggregate or non-reference types: Arrays and structs
- Interface types
Boolean
Boolean is a 1 bit type with either true
or false
value. The default value of boolean variable is false.
String
A string data type is the sequence of UTF-8 encoded characters with a definite length used to represent a text. It is declared by enclosing between double quotes or back quotes e.g. "Hello" and 'Hello' are strings in go.
Numeric types
Numeric types can be signed integers, unsigned integers, and floats.
Signed integers can store positive or negative numeric values. The following table lists signed integer types:
Type | Size | Range |
---|---|---|
int | Platform dependent. 32 bits in 32 bit systems and 64 bit in 64 bit systems | -2147483648 to 2147483647 in 32 bit platform -9223372036854775808 to 9223372036854775807 in 64 bit platform |
int8 | 8 bits/1 byte | -128 to 127 |
int16 | 16 bits/2 byte | -32768 to 32767 |
int32 (rune) | 32 bits/4 byte | -2147483648 to 2147483647 |
int64 | 64 bits/8 byte | -9223372036854775808 to 9223372036854775807 |
Unsigned integer types can store only positive numeric values.The following table lists unsigned integer types:
Type | Size | Range |
---|---|---|
Uint | Platform dependent: 32 bits in 32 bit platform and 64 bit in 64 bit platform | 0 to 4294967295 in 32 bit platform 0 to 18446744073709551615 in 64 bit platform |
uint8 (byte) | 8 bits/1 byte | 0 to 255 |
uint16 | 16 bits/2 byte | 0 to 65535 |
uint32 | 32 bits/4 byte | 0 to 4294967295 |
uInt64 | 64 bits/8 byte | 0 to 18446744073709551615 |
Uintptr | Platform dependent | Platform dependent |
Note: If you do not specify a type for numeric values, the defulat type will be int
. The uintptr
is an unsigned integer type that can hold a pointer address.
Float Types
The float types store positive and negative numeric values with a decimal point, e.g. 15.5, -20.55, or 4578.5568. The following table lists floating point types:
Type | Size | Range |
---|---|---|
Float32 | 32 bits or 4 bytes | -3.4e+38 to 3.4e+38. |
Float64 | 64 bits/8 bytes | -1.7e+308 to +1.7e+308. |
The following table lists complex numeric types:
Type | Size | Example |
---|---|---|
Complex64 | Consists of float32 as real and imaginary parts | 5 + 6i |
Complex128 | Consists of float64 as real and imaginary parts | -10.4 +23.41i |
Let's see how to declare variables of different data types in the next page.