Data Types
Below are all the built-in types in Go for reference. For more details, visit Reference Manual - Types.
Boolean Type
Boolean type only has true and false values.
| Type | Description |
|---|---|
bool | true is truthy, false is falsy |
TIP
In Go, integer 0 does not represent false, and non-zero integers do not represent true. Numbers cannot replace boolean values for logical judgment. They are completely different types.
Integer Types
Go assigns different types for integers of different bit lengths, mainly divided into unsigned integers and signed integers.
| Number | Type and Description |
|---|---|
uint8 | Unsigned 8-bit integer |
uint16 | Unsigned 16-bit integer |
uint32 | Unsigned 32-bit integer |
uint64 | Unsigned 64-bit integer |
int8 | Signed 8-bit integer |
int16 | Signed 16-bit integer |
int32 | Signed 32-bit integer |
int64 | Signed 64-bit integer |
uint | Unsigned integer, at least 32 bits |
int | Integer, at least 32 bits |
uintptr | Equivalent to unsigned 64-bit integer, but specifically for pointer arithmetic to store raw pointer addresses. |
Floating Point Types
IEEE-754 floating point numbers, mainly divided into single-precision floating point and double-precision floating point.
| Type | Type and Description |
|---|---|
float32 | IEEE-754 32-bit floating point |
float64 | IEEE-754 64-bit floating point |
Complex Types
| Type | Description |
|---|---|
complex128 | 64-bit real and imaginary |
complex64 | 32-bit real and imaginary |
Character Types
Go strings are fully compatible with UTF-8.
| Type | Description |
|---|---|
byte | Equivalent to uint8, can represent ANSCII characters |
rune | Equivalent to int32, can represent Unicode characters |
string | String is a byte sequence, can be converted to []byte type (byte slice) |
Derived Types
| Type | Example |
|---|---|
| Array | [5]int, integer array of length 5 |
| Slice | []float64, 64-bit floating point slice |
| Map | map[string]int, map with string keys and int values |
| Struct | type Gopher struct{}, Gopher struct |
| Pointer | *int, a pointer to an integer |
| Function | type f func(), a function type with no parameters and no return value |
| Interface | type Gopher interface{}, Gopher interface |
| Channel | chan int, integer channel |
Zero Values
The official documentation calls zero values "zero value". Zero value is not just the number zero literally, but rather the empty value or default value of a type.
| Type | Zero Value |
|---|---|
| Numeric types | 0 |
| Boolean types | false |
| String types | "" |
| Array | Collection of zero values of the corresponding type with fixed length |
| Struct | Struct with all fields set to zero values |
| Slice, map, function, interface, channel, pointer | nil |
nil
nil is similar to none or null in other languages, but not exactly the same. nil is only the zero value of some reference types and does not belong to any type. From the source code, you can see that nil is just a variable.
var nil TypeAnd statements like nil == nil cannot be compiled.
