Go (Golang) Data Types & Data Structures — Complete Guide
Introduction
Go is a statically typed, compiled language designed for:
-
Simplicity
-
Performance
-
Concurrency
-
Predictable memory behavior
In Go:
-
Every variable has a fixed type
-
Types are checked at compile time
-
Zero values are automatically assigned
Categories of Go Data Types
Go data types can be grouped into:
-
Basic Types
-
Composite Types
-
Reference Types
-
Interface Types
1️⃣ Basic Data Types
a) Integer Types
Common integer types:
-
int,int8,int16,int32,int64 -
uint,uint8(byte),uint16,uint32,uint64
b) Floating Point Types
c) Boolean
d) String
Strings are immutable in Go.
Iterating characters:
2️⃣ Array (Fixed Size)
Arrays have fixed length.
Access:
⚠️ Arrays are rarely used directly in Go.
3️⃣ Slice (MOST IMPORTANT)
Slices are dynamic, flexible views over arrays.
Create a slice
Append
Access
Update
Iterate
Slice internals (Interview Gold)
A slice has:
4️⃣ Map (Key–Value Store)
Maps store unordered key–value pairs.
Create map
Add / Update
Retrieve
Check existence
Delete
5️⃣ Struct (Custom Data Type)
Structs group related data.
Create and use:
Pointer to struct:
6️⃣ List (container/list – Doubly Linked List)
Go provides a linked list via container/list.
Add elements
Iterate
Use cases:
-
Frequent insert/delete
-
No random access
7️⃣ Set (Using map)
Go has no built-in set, but maps are used.
Add
Check
Delete
8️⃣ Pointer Types
Pointers store memory addresses.
Used for:
-
Performance
-
Mutability
-
Struct updates
-
Large data passing
9️⃣ Interface (Polymorphism)
Interfaces define behavior.
Implement interface:
Use:
🔟 Zero Values (Very Important)
Go automatically assigns zero values.
| Type | Zero Value |
|---|---|
| int | 0 |
| float | 0.0 |
| bool | false |
| string | "" |
| slice | nil |
| map | nil |
| pointer | nil |
1️⃣1️⃣ Mutable vs Immutable
| Type | Mutable |
|---|---|
| int, float, string | ❌ |
| slice | ✅ |
| map | ✅ |
| struct | ✅ |
| array | ❌ (value copy) |
1️⃣2️⃣ Common Real-World Examples
List of users
Lookup by ID
Unique IDs
1️⃣3️⃣ Summary Table
| Type | Best Use |
|---|---|
| int / float | Numbers |
| string | Text |
| array | Fixed size |
| slice | Dynamic lists |
| map | Fast lookup |
| struct | Custom objects |
| list | Frequent inserts |
| interface | Polymorphism |
| pointer | Performance |
1️⃣4️⃣ Interview One-Line Summary ⭐
Go provides strong, static data types with powerful composite structures like slices, maps, structs, and interfaces, enabling efficient, predictable, and concurrent-safe programs.
No comments:
Post a Comment