Go Data Types
Go - Data Types
Section titled “Go - Data Types”Data types in Go specify the kind of values a variable can hold and the operations that can be performed on those values. Go is a statically-typed language, meaning variable types are known at compile time.
Go’s data types can be broadly categorized as follows:
| Category | Description and Examples |
|---|---|
| Boolean Types | bool: Represents a boolean value, either true or false.Example: var isActive bool = true |
| Numeric Types | Represent numbers. Subdivided into: - Integer Types: int8, int16, int32, int64, int (signed); uint8, uint16, uint32, uint64, uint (unsigned). byte is an alias for uint8. rune is an alias for int32 (represents a Unicode code point).Example: var age int = 30, var temperature int8 = -5, var unicodeChar rune = 'G'- Floating-Point Types: float32, float64 (IEEE-754 standard).Example: var price float64 = 19.99- Complex Types: complex64 (float32 real and imaginary parts), complex128 (float64 real and imaginary parts).Example: var c complex128 = 3 + 4i |
| String Types | string: Represents a sequence of bytes, typically UTF-8 encoded text. Strings are immutable.Example: var name string = "Golang" |
| Derived/Composite Types | Constructed from basic types or other composite types: - Pointer Types: e.g., *int (a pointer to an integer). Holds a memory address.- Array Types: e.g., [5]int (an array of 5 integers). Fixed-size.- Slice Types: e.g., []int (a slice of integers). Dynamically-sized view of an array.- Structure Types ( struct): e.g., type Point struct { X, Y int }. Groups fields.- Function Types: Represents the signature of a function. - Interface Types: Defines a set of method signatures. - Map Types: e.g., map[string]int. Unordered key-value pairs.- Channel Types: e.g., chan int. Used for goroutine communication. |
Integer Types Detailed
Section titled “Integer Types Detailed”Go provides architecture-independent integer types with fixed sizes, as well as implementation-dependent int and uint types.
| Type | Size | Range (Approximate) |
|---|---|---|
int8 | 8-bit | -128 to 127 |
uint8 (byte) | 8-bit | 0 to 255 |
int16 | 16-bit | -32,768 to 32,767 |
uint16 | 16-bit | 0 to 65,535 |
int32 (rune) | 32-bit | -2.1 billion to 2.1 billion |
uint32 | 32-bit | 0 to 4.2 billion |
int64 | 64-bit | -9 quintillion to 9 quintillion |
uint64 | 64-bit | 0 to 18 quintillion |
int | 32 or 64 bits | Depends on system architecture |
uint | 32 or 64 bits | Depends on system architecture |
Floating-Point Types Detailed
Section titled “Floating-Point Types Detailed”Go supports standard IEEE-754 floating-point numbers.
| Type | Precision | Description |
|---|---|---|
float32 | Approx. 6-9 decimal digits | 32-bit floating-point number |
float64 | Approx. 15-17 decimal digits | 64-bit floating-point number (default for literals like 3.14) |
Complex Types Detailed
Section titled “Complex Types Detailed”For complex number arithmetic.
| Type | Component Type | Description |
|---|---|---|
complex64 | float32 | Real and imaginary parts are float32 |
complex128 | float64 | Real and imaginary parts are float64 |
Other Special Numeric Aliases:
Section titled “Other Special Numeric Aliases:”byte: Alias foruint8. Commonly used to represent byte data.rune: Alias forint32. Represents a Unicode code point, often used when working with characters and strings.
Additionally, uintptr is an unsigned integer type large enough to store the uninterpreted bits of a pointer value. Its use is typically confined to low-level programming with the unsafe package.
Choosing the right data type is important for memory efficiency and program correctness. For example, using int8 for a value that will never exceed 100 saves memory compared to int64.