Chapter 22: Create Slice

Create slice.

In almost every real Go program you will create slices many times per file — so understanding all the different ways to do it, together with their advantages, memory behavior and common pitfalls, is super valuable.

Today I’ll explain Create Slice like we’re sitting together with laptop open: theory first, then every realistic creation pattern with code + output + explanation + when-you-should-use-this-style.

Quick Reminder – What is a slice really?

A slice is three numbers + a hidden pointer:

text

Every time you create a slice, you’re either:

  • creating a new backing array + slice header, or
  • creating a slice header that points into an existing array (or another slice)

All Realistic Ways to Create a Slice (2025–2026 style)

# Way to create Syntax example Backing array? len / cap after creation Most common use-case Idiomatic level
1 Slice literal []int{1,2,3} Yes – new len=3, cap=3 Small fixed lists you write by hand ★★★★★
2 make( []T, length ) make([]int, 5) Yes – new len=5, cap=5 When you know exact size and want zeros ★★★★☆
3 make( []T, len, cap ) make([]byte, 0, 4096) Yes – new len=0, cap=4096 Pre-allocate capacity (buffers, builders, append loops) ★★★★★
4 Slice an array arr[:] or arr[2:7] No – uses existing depends on slice Converting fixed array to dynamic slice ★★★☆☆
5 Slice an existing slice bigSlice[low:high] No – shares backing depends on slice Window / sub-range view ★★★★★
6 Slice an existing slice with cap limit s[low:high:high] No – shares backing len=high-low, cap=high-low Prevent append from touching tail elements ★★★★☆
7 Nil slice var s []int or []int(nil) No backing array len=0, cap=0 “no value” / optional / zero-value ★★★★★
8 Empty non-nil slice []int{} or make([]int, 0) Tiny backing array len=0, cap=0 or small When you want non-nil but empty ★★★☆☆

Detailed Examples – Run These!

Go

Typical output summary:

text

Quick Decision Guide – Which creation style to choose?

Situation / Goal Recommended way Why
You know the exact small values right now []int{1,2,3} Most readable
You know exact size and want zero values make([]T, size) Clear intent
You will append a lot → want to avoid reallocations make([]T, 0, estimatedCapacity) Best performance
Converting fixed array to dynamic array[:] Zero-cost view
Taking a window / sub-range s[low:high] or s[low:high:high] Very cheap
Meaning “no value” / optional field var s []T or []T(nil) idiomatic zero value
Want empty but non-nil (rare) []T{} or make([]T, 0) Only when JSON marshaling matters

Your Mini Practice Task Right Now

Try to create these four slices and print their len/cap:

  1. A nil slice of float64
  2. An empty non-nil slice of strings
  3. A pre-allocated buffer of 0 length but 1 MB capacity of bytes
  4. A view of the middle 4 elements from this slice: data := []int{10,20,30,40,50,60,70,80,90}

Which one has cap > len? Why?

Any part still confusing?

  • What exactly happens inside append when cap is full?
  • Difference between nil and len==0 in JSON / function parameters?
  • 2D slices ([][]int) creation patterns?
  • Or ready for append in depth next?

Keep typing & running these examples — slice creation is something you’ll do 50+ times per real project, so getting comfortable now pays off massively.

You’re doing awesome — let’s keep building! 💻🇮🇳🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *