×

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 Access Elements of a Slice

Last Updated : May 04, 2025

In Go, you can access elements of a slice using index positions, just like arrays. The index starts at 0, and you can use square brackets to retrieve or update a value at a specific position.

Accessing Elements Using Index

To access an element from a slice, use its index inside square brackets.

Example

In this example, we are accessing and printing elements of a slice using index positions:

package main
import "fmt"

func main() {
    cities := []string{"Delhi", "Mumbai", "Bengaluru"}

    fmt.Println("First city:", cities[0])
    fmt.Println("Second city:", cities[1])
    fmt.Println("Third city:", cities[2])
}

When executed, this program outputs:

First city: Delhi
Second city: Mumbai
Third city: Bengaluru

Modifying Elements in a Slice

You can also change the value of a slice element by assigning a new value to a specific index.

Example

In this example, we are modifying an element of a slice by updating the value at a specific index and then printing the updated slice:

package main
import "fmt"

func main() {
    students := []string{"Aarav", "Meena", "Ravi"}
    students[1] = "Pooja" // Changing the second element

    fmt.Println("Updated students list:", students)
}

When executed, this program outputs:

Updated students list: [Aarav Pooja Ravi]

Accessing Elements in a Loop

You can use a for loop or for...range loop to iterate and access each element of a slice.

Example

In this example, we are iterating over a slice using a for loop to access and print each element along with its index:

package main
import "fmt"

func main() {
    numbers := []int{100, 200, 300}

    // Using for loop
    for i := 0; i < len(numbers); i++ {
        fmt.Println("Element at index", i, "is", numbers[i])
    }
}

When executed, this program outputs:

Element at index 0 is 100
Element at index 1 is 200
Element at index 2 is 300

Using range with Slices

The range keyword allows iterating over slices and retrieving both index and value conveniently.

Example

In this example, we are using a for range loop to iterate over a slice and print each element along with its index:

package main
import "fmt"

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

    for index, fruit := range fruits {
        fmt.Printf("Fruit at index %d: %s\n", index, fruit)
    }
}

When executed, this program outputs:

Fruit at index 0: Apple
Fruit at index 1: Orange
Fruit at index 2: Mango

Exercise

Select the correct option to complete each statement about accessing elements of a slice in Go.

  1. To access the element at index i in a Go slice, you use the syntax ___.
  2. If you try to access an index i that is out of range in a Go slice, it will result in a ___.
  3. To get a sub-slice from index i to j of a slice, the syntax is ___.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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