×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Go Slices

Last Updated : May 04, 2025

Unlike arrays, Go slices are dynamically-sized. Slices are much more commonly used than arrays in Go programming.

Declaring and Initializing Slices

You can declare and initialize slices using literals or the built-in make() function.

Example

In this example, we are declaring and initializing a slice of integers and printing its contents:

package main
import "fmt"

func main() {
    // Declaring and initializing a slice
    numbers := []int{10, 20, 30, 40}

    fmt.Println("Slice:", numbers)
}

When executed, this program outputs:

Slice: [10 20 30 40]

Creating a Slice from an Array

You can create a slice by slicing an existing array using a range of indices.

Example

In this example, we are creating a slice from an existing array by specifying a range of indices, and then printing both the original array and the resulting slice:

package main
import "fmt"

func main() {
    arr := [5]int{1, 2, 3, 4, 5}

    slice := arr[1:4] // From index 1 to 3 (excluding 4)

    fmt.Println("Array:", arr)
    fmt.Println("Slice:", slice)
}

When executed, this program outputs:

Array: [1 2 3 4 5]
Slice: [2 3 4]

Using the make() Function

The make() function creates a slice with a specific length and capacity.

Example

In this example, we are using the make() function to create a slice of integers with a length of 3 and a capacity of 5, and then printing its contents, length, and capacity:

package main
import "fmt"

func main() {
    slice := make([]int, 3, 5) // Length 3, Capacity 5

    fmt.Println("Slice:", slice)
    fmt.Println("Length:", len(slice))
    fmt.Println("Capacity:", cap(slice))
}

When executed, this program outputs:

Slice: [0 0 0]
Length: 3
Capacity: 5

Appending to a Slice

Slices can grow in size using the built-in append() function.

Example

In this example, we are initializing a slice of fruits and then using the append() function to add more elements to the slice before printing the final list:

package main
import "fmt"

func main() {
    fruits := []string{"Mango", "Banana"}
    fruits = append(fruits, "Guava", "Apple")

    fmt.Println("Fruits:", fruits)
}

When executed, this program outputs:

Fruits: [Mango Banana Guava Apple]

Copying Slices

Use the copy() function to copy elements from one slice to another.

Example

In this example, we are copying the contents of one slice into another using the copy() function and then printing both the original and the copied slices:

package main
import "fmt"

func main() {
    original := []int{5, 10, 15}
    duplicate := make([]int, len(original))

    copy(duplicate, original)

    fmt.Println("Original Slice:", original)
    fmt.Println("Copied Slice:", duplicate)
}

When executed, this program outputs:

Original Slice: [5 10 15]
Copied Slice: [5 10 15]

Go Slices Exercise

Select the correct option to complete each statement about slices in the Go programming language.

  1. A slice in Go is a dynamically-sized view into the elements of an ___.
  2. The built-in append() function is used to ___ to a slice.
  3. The len() function returns the ___ of a slice.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.