0PricingLogin
Go Academy · Lesson

Slices: Dynamic Lists

make, append, copy, len and cap

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))
}

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