GoLang

How to iterate over the Slice in the Go Programming | Go Programming Tutorial

How to iterate over the Slice in the Go Programming | Go Programming Tutorial

How to iterate over the Slice in the Go Programming | Go Programming Tutorial

In this article, you will learn about how to iterate over the Slice elements in the Go Programming Language.

The user can iterate over the elements in the Slice with the help of the following ways:

  • Using for loop: It is the easiest way to iterate a slice as it is appearing.

Example:

package main
import "fmt"
 
func main() {
 
    // Create a slice
    slice_ele := []string{"This", "is", "the", "tutorials","link"}
 
    // Iterate using for loop
    for i := 0; i < len(slice_ele); i++ {
        fmt.Println(slice_ele[i])
    }
}

Output:

This

is 

the

tutorials

Link

  • Using range in for loop: It is permitted to iterate over a slice using a range in the for a loop. By using a range in for loop, you can get the index and the element value. 

Example:

package main
import "fmt"
 
func main() {
 
    // Creating a slice
    s := []string{"This", "is", "the", "Tutorials","Link"}
 
    // Iterate sliceusing range in for loop
    for i, ele := range s {
        fmt.Printf("Index = %d and element = %s\n", i+3, ele)
    }
}

Output:

Index = 3 and element = This

Index = 4 and element = is

Index = 5 and element = the

Index = 6 and element = Tutorials

Index = 7 and element = Link

  • Using a blank identifier in the for loop: In the range for loop, if you don't want to get the index value of the elements then you can use a blank identifier(_) in place of the index variable

Example:

package main
import "fmt"
 
func main() {
 
    // Create a slice
    my_s := []string{"This", "is", "the","Tutorials", "Link"}
 
    // Iterate sliceusing range in for loop
    // without index
    for _, ele := range my_s {
        fmt.Printf("Element = %s\n", ele)
    }
}

OUTPUT:

Element = This 

Element = is

Element = the

Element = Tutorials

Element = Link

Points to be Remembered:

  • Zero Value Slice: In GoLang, the user can create a nil slice that does not contain any element in it. The capacity and the length of the slice are zero. It does not have any array reference.
  • Modifying Slice: As we know, the slice is of reference type it can give the reference of an underlying array. So, if we change a few elements in the slice, then the changes will also take place in the referenced array. In simple words, if you make any changes in the slice, then it will also be shown in the array. 
  • Comparison of Slice: In Slice, the user is only allowed to use the == operator to verify whether the given slice is null or not. If the user tries to use the == operator to compare the two slices then it will give a compilation error.
  • Multidimensional Slices: It is very much similar to the multidimensional array but the only difference between the two is that the slice does not contain the size. 
  • Sorting of Slice: Just like an array, the user can sort the elements of the slice. The Go Language has a standard library that provides the sort package that contains different types of sorting methods for sorting the slice of different types. These functions always sort the items available in slices in ascending order.
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.