🍰 Go – Slices Explained with Syntax, Examples, and Best Practices (2025 Guide)
🧲 Introduction – What Are Slices in Go?
In Go, slices are the dynamic alternative to arrays. A slice is a reference to an underlying array and supports flexible, resizable views of data. Slices are the default data structure for working with lists in Go due to their simplicity, performance, and ease of use.
🎯 In this section, you’ll learn:
- How to declare, initialize, and access slices
- The difference between slices and arrays
- Slice operations: append, copy, slicing, and length/capacity
- Best practices and performance tips
✅ Slice Declaration and Initialization
🔹 Declare a Slice Using Literal
nums := []int{10, 20, 30}
fmt.Println(nums)
📤 Output:
[10 20 30]
✅ The []int indicates a slice of integers.
🔹 Create with make() Function
slice := make([]string, 3)
fmt.Println(slice) // ["" "" ""]
✅ Creates a slice with length 3, initialized to zero values.
🔁 Slicing an Array
arr := [5]int{1, 2, 3, 4, 5}
s := arr[1:4]
fmt.Println(s)
📤 Output:
[2 3 4]
✅ Slices are views over arrays and can be created from them using [start:end].
➕ Append Elements to a Slice
nums := []int{1, 2}
nums = append(nums, 3, 4)
fmt.Println(nums)
📤 Output:
[1 2 3 4]
✅ append() returns a new slice with added elements. It may allocate a new array if needed.
📤 Copy One Slice to Another
src := []int{10, 20, 30}
dst := make([]int, len(src))
copy(dst, src)
fmt.Println(dst)
📤 Output:
[10 20 30]
✅ copy(dst, src) copies elements from one slice to another.
🔍 Length and Capacity of Slices
s := []int{1, 2, 3}
fmt.Println(len(s)) // 3
fmt.Println(cap(s)) // 3 (capacity equals length here)
✅ len() gives current size, cap() shows max potential size before reallocation.
✂️ Reslicing (Sub-slicing)
slice := []string{"a", "b", "c", "d", "e"}
sub := slice[1:4]
fmt.Println(sub) // [b c d]
✅ You can create sub-slices dynamically using [low:high] syntax.
⚖️ Slice vs Array in Go
| Feature | Slice | Array |
|---|---|---|
| Size | Dynamic | Fixed at compile time |
| Type | Reference to array | Value type (copy on assignment) |
| Append Support | ✅ Yes | ❌ No |
| Use Case | Lists, dynamic collections | Static-size buffers, low-level data |
🧠 Best Practices
- ✅ Use slices for dynamic data
- ✅ Use
append()instead of manual index assignment - ❌ Avoid slicing beyond capacity
- ✅ Use
copy()for manual duplication
📌 Summary – Recap & Next Steps
Go slices provide a powerful and idiomatic way to work with variable-length data. They simplify array handling while offering great performance and flexibility.
🔍 Key Takeaways:
- Slices are dynamic, unlike fixed-size arrays
- Created from arrays or using
make() - Use
append(),copy(), and slicing operations freely - Always monitor
len()andcap()for performance tuning
⚙️ Next: Dive into Multidimensional Slices or explore Go Maps for key-value storage.
❓ FAQs – Go Slices
❓ How are slices different from arrays in Go?
✅ Slices are dynamic and reference-based, while arrays are fixed-size and copied by value.
❓ Can slices be resized?
✅ Yes. Use append() to add elements dynamically.
❓ What happens when you slice beyond the capacity?
❌ A runtime panic occurs. Always ensure bounds are safe.
❓ Do slices share memory with the underlying array?
✅ Yes. Changes to the slice affect the original array and vice versa (unless a new array is created).
❓ How do I copy a slice without affecting the original?
✅ Use copy() to create a new slice with the same values.
Share Now :
