0Pricing
Go Academy · Lesson

Slices: Dynamic Lists

make, append, copy, len and cap

Slices: Dynamic Lists is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Slice?

A slice is a dynamically-sized view into an underlying array. It has three components: a pointer to the array, a length, and a capacity.

  • Slices are reference types — multiple slices can share the same backing array
  • Most list-like work in Go uses slices, not arrays

Slice Literals and make

Create slices with a literal or make:

package main
import "fmt"

func main() {
    nums := []int{1, 2, 3, 4, 5}      // literal
    strs := make([]string, 3)          // len=3, cap=3, zero values
    buf  := make([]byte, 0, 64)        // len=0, cap=64
    fmt.Println(nums, strs, len(buf), cap(buf))
}

len and cap

len returns the number of elements currently in the slice. cap returns the capacity of the underlying array from the slice's start pointer.

package main
import "fmt"

func main() {
    s := make([]int, 3, 8)
    fmt.Println(len(s)) // 3
    fmt.Println(cap(s)) // 8
}

append: Growing a Slice

append adds elements to a slice, allocating a new backing array when capacity is exceeded:

package main
import "fmt"

func main() {
    s := []int{1, 2, 3}
    s = append(s, 4, 5)
    s = append(s, []int{6, 7, 8}...)
    fmt.Println(s) // [1 2 3 4 5 6 7 8]
}

Slicing a Slice

Use the [low:high] syntax to create a sub-slice (shares the backing array):

package main
import "fmt"

func main() {
    a := []int{0, 1, 2, 3, 4, 5}
    b := a[1:4]   // [1 2 3]
    c := a[:3]    // [0 1 2]
    d := a[3:]    // [3 4 5]
    fmt.Println(b, c, d)
}

copy: Independent Slices

copy copies elements between slices and returns the number copied. The destination and source don't share memory:

package main
import "fmt"

func main() {
    src := []int{1, 2, 3}
    dst := make([]int, len(src))
    n := copy(dst, src)
    dst[0] = 99
    fmt.Println(src, dst, n) // [1 2 3] [99 2 3] 3
}

nil Slice vs Empty Slice

A nil slice has no backing array. An empty slice has a backing array but length 0. Both have len == 0:

package main
import "fmt"

func main() {
    var nilSlice []int
    emptySlice  := []int{}
    fmt.Println(nilSlice  == nil) // true
    fmt.Println(emptySlice == nil) // false
    fmt.Println(len(nilSlice), len(emptySlice)) // 0 0
    nilSlice = append(nilSlice, 1) // safe!
}

Deleting an Element

Go has no built-in delete for slices. The idiomatic approach uses append:

package main
import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    i := 2 // delete index 2
    s = append(s[:i], s[i+1:]...)
    fmt.Println(s) // [1 2 4 5]
}

Inserting an Element

Insert at position i by combining append and slicing:

package main
import "fmt"

func main() {
    s := []int{1, 2, 4, 5}
    i := 2
    s = append(s[:i+1], s[i:]...)
    s[i] = 3
    fmt.Println(s) // [1 2 3 4 5]
}

Slice Gotcha: Shared Backing Array

Sub-slices share the backing array — modifying one affects the other until a reallocation happens:

package main
import "fmt"

func main() {
    a := []int{1, 2, 3, 4}
    b := a[:2]   // shares backing array
    b[0] = 99
    fmt.Println(a) // [99 2 3 4] — a is changed!
}

Quick Check

What does append return?

Recap: Slices

Key takeaways:

  • Slices = pointer + len + cap; they wrap arrays
  • Use make to pre-allocate capacity
  • Always assign the result of append
  • copy creates independent slices
  • Be aware of shared backing arrays with sub-slices

Practice Prompt

Create a slice of 5 ints with make, append three more values, then copy it to a new slice and modify the copy without affecting the original. Print both.

Frequently asked questions

Is the “Slices: Dynamic Lists” lesson free?

Yes — the full text of “Slices: Dynamic Lists” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Slices: Dynamic Lists”?

make, append, copy, len and cap You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Slices: Dynamic Lists” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays: Fixed-Length Collections
  2. Slices: Dynamic Lists
  3. Maps: Key-Value Stores
  4. Iterating Collections with range
← Back to Go Academy