codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

12 / 72 topics
11Arrays12Slices13Maps14Structs15Methods16Interfaces
Tutorials/Go (Golang)/Slices
🐹Go (Golang)

Slices

Updated 2026-04-20
3 min read

Slices

Introduction

In Go, a slice is a dynamically-sized, flexible view into the elements of an array. Unlike arrays, slices are more commonly used because they provide more functionality and flexibility. They are a powerful feature that allows you to work with sequences of data efficiently.

Understanding Slices

A slice in Go consists of three components:

  1. Pointer: Points to the first element of the underlying array.
  2. Length: The number of elements in the slice.
  3. Capacity: The maximum number of elements that the slice can hold without allocating a new array.

Slices are built on top of arrays and provide a way to access a subsequence of an array's elements. They are more flexible than arrays because they can grow or shrink in size, unlike arrays which have a fixed size.

Creating Slices

Using make Function

The make function is used to create a slice with a specified length and capacity.

package main

import "fmt"

func main() {
    // Create a slice of integers with length 5 and capacity 10
    s := make([]int, 5, 10)
    fmt.Println("Slice:", s)          // Output: Slice: [0 0 0 0 0]
    fmt.Println("Length:", len(s))     // Output: Length: 5
    fmt.Println("Capacity:", cap(s)) // Output: Capacity: 10
}

Using Array Literals

You can create a slice from an array literal.

package main

import "fmt"

func main() {
    // Create an array and then create a slice from it
    arr := [5]int{1, 2, 3, 4, 5}
    s := arr[1:3]
    fmt.Println("Slice:", s)          // Output: Slice: [2 3]
    fmt.Println("Length:", len(s))     // Output: Length: 2
    fmt.Println("Capacity:", cap(s)) // Output: Capacity: 4
}

Using append Function

The append function is used to add elements to a slice. It returns a new slice if the underlying array needs to be resized.

package main

import "fmt"

func main() {
    s := []int{1, 2, 3}
    s = append(s, 4)
    fmt.Println("Slice:", s)          // Output: Slice: [1 2 3 4]
    fmt.Println("Length:", len(s))     // Output: Length: 4
    fmt.Println("Capacity:", cap(s)) // Output: Capacity: 6
}

Slicing

Slicing is the process of creating a new slice from an existing slice or array. The syntax for slicing is slice[start:end], where start is inclusive and end is exclusive.

package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    subSlice := s[1:3]
    fmt.Println("Subslice:", subSlice) // Output: Subslice: [2 3]
}

Slicing Rules

  • If start is omitted, it defaults to 0.
  • If end is omitted, it defaults to the length of the slice.
package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    fmt.Println("From start:", s[:3]) // Output: From start: [1 2 3]
    fmt.Println("To end:", s[2:])   // Output: To end: [3 4 5]
    fmt.Println("Whole slice:", s[:]) // Output: Whole slice: [1 2 3 4 5]
}

Slice Operations

Appending Multiple Elements

You can append multiple elements to a slice using the append function.

package main

import "fmt"

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

Copying Slices

The copy function is used to copy elements from one slice to another. It returns the number of elements copied.

package main

import "fmt"

func main() {
    src := []int{1, 2, 3}
    dst := make([]int, len(src))
    n := copy(dst, src)
    fmt.Println("Copied elements:", n) // Output: Copied elements: 3
    fmt.Println("Destination slice:", dst) // Output: Destination slice: [1 2 3]
}

Reslicing

Reslicing a slice can change its length and capacity.

package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    subSlice := s[1:3]
    fmt.Println("Subslice:", subSlice) // Output: Subslice: [2 3]

    // Reslice to increase length and capacity
    subSlice = subSlice[:cap(subSlice)]
    fmt.Println("Resliced subslice:", subSlice) // Output: Resliced subslice: [2 3 4 5]
}

Best Practices

  1. Avoid Slicing Beyond Capacity: Always ensure that the slice operations do not exceed the capacity of the underlying array to avoid unexpected behavior.

  2. Use make for Efficient Slice Creation: When you know the initial size and capacity, use the make function to create slices efficiently.

  3. Be Careful with Append: The append function can allocate a new array if the underlying array is full. Always check the capacity before appending large numbers of elements.

  4. Copy Slices When Needed: Use the copy function when you need to copy elements from one slice to another, especially when dealing with concurrent access.

  5. Use Slice Literals Sparingly: While slice literals are convenient, they can lead to unnecessary allocations if used excessively. Prefer using make or slicing existing slices when possible.

Conclusion

Slices in Go provide a powerful and flexible way to work with sequences of data. Understanding how to create, manipulate, and use slices effectively is crucial for writing efficient and idiomatic Go code. By following the best practices outlined in this tutorial, you can make the most out of slices in your Go applications.

Further Reading

  • Go Slices: usage and internals
  • Effective Go - Slices

PreviousArraysNext Maps

Recommended Gear

ArraysMaps