Overview of Go Type System
https://go101.org/article/type-system-overview.html
Basic Types
Built-in string type: string
.
Built-in boolean type: bool
.
Built-in numeric types:int8
, uint8 (byte)
, int16
, uint16
, int32 (rune)
, uint32
, int64
, uint64
, int
, uint
, uintptr
.float32
, float64
.complex64
, complex128
.
Note, byte
is a built-in alias of uint8
, and rune
is a built-in alias of int32
. We can also declare custom type aliases (see below).
Composite Types
pointer types - C pointer alike.
struct types - C struct alike.
function types - functions are first-class types in Go.
container types:
- array types - fixed-length container types.
- slice type - dynamic-length and dynamic-capacity container types.
- map types - maps are associative arrays (or dictionaries). The standard Go compiler implements maps as hashtables.
channel types - channels are used to synchronize data among goroutines (the green threads in Go).
interface types - interfaces play a key role in reflection and polymorphism.
1 | // Assume T is an arbitrary type and Tkey is |
Type Definitions
1 | // Define a solo new type. |
Type Alias Declarations
1 | type ( |
Defined Types vs. Non-Defined Types
A defined type is a type defined in a type definition.
All basic types are defined. A non-defined type must be a composite type.
In the following example. type alias C and type literal []string both represent the same non-defined types, but type A and type alias B both represent the same defined type.
1 | type A []string |