Home »
Golang
Go Slice Ranges
Last Updated : May 05, 2025
In Go, you can access a portion of a slice using the slice range syntax. This allows you to create a new slice from an existing one, either by specifying a range of indices or by leaving some indices unspecified.
Basic Slice Range Syntax
The basic slice range syntax involves specifying a start and an end index. You can create a new slice by slicing an existing slice within a specified range of indices.
Example
In this example, we create a new slice by specifying the start and end indices for the range.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai", "Bengaluru", "Kolkata"}
// Create a slice from index 1 to index 3 (Mumbai, Chennai, Bengaluru)
newCities := cities[1:4]
fmt.Println("Original cities:", cities)
fmt.Println("New slice of cities:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai Bengaluru Kolkata]
New slice of cities: [Mumbai Chennai Bengaluru]
Omit the Start Index
If you omit the start index in the slice range, Go assumes you want to start from index 0.
Example
In this example, we omit the start index, which creates a slice starting from the beginning of the original slice.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai", "Bengaluru", "Kolkata"}
// Slice starting from index 0 to index 3 (Delhi, Mumbai, Chennai)
newCities := cities[:3]
fmt.Println("Original cities:", cities)
fmt.Println("New slice of cities:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai Bengaluru Kolkata]
New slice of cities: [Delhi Mumbai Chennai]
Omit the End Index
If you omit the end index in the slice range, Go assumes you want to include all elements from the specified start index to the end of the slice.
Example
In this example, we omit the end index, so the slice starts from the second index (Chennai) and includes all elements until the end of the original slice.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai", "Bengaluru", "Kolkata"}
// Slice starting from index 2 to the end (Chennai, Bengaluru, Kolkata)
newCities := cities[2:]
fmt.Println("Original cities:", cities)
fmt.Println("New slice of cities:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai Bengaluru Kolkata]
New slice of cities: [Chennai Bengaluru Kolkata]
Omit Both the Start and End Indices
You can also omit both the start and end indices in the slice range, which results in copying the entire slice.
Example
In this example, we omit both the start and end indices, which copies the entire slice.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai", "Bengaluru", "Kolkata"}
// Slice the entire slice
newCities := cities[:]
fmt.Println("Original cities:", cities)
fmt.Println("Full slice:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai Bengaluru Kolkata]
Full slice: [Delhi Mumbai Chennai Bengaluru Kolkata]
Slice Range with Negative Indices
You can use negative indices in slice ranges. A negative index counts from the end of the slice, where -1 is the last element, -2 is the second last, and so on.
Example
In this example, we use negative indices to slice the last two elements of the original slice.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai", "Bengaluru", "Kolkata"}
// Slice the last two elements
newCities := cities[-2:]
fmt.Println("Original cities:", cities)
fmt.Println("Last two cities:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai Bengaluru Kolkata]
Last two cities: [Bengaluru Kolkata]
Exercise
Select the correct option to complete each statement about slice ranges in Go.
- To iterate over a slice s using a range in Go, the syntax is ___.
- When using a range over a slice, the value v refers to ___.
- If you only need the index and not the value when iterating over a slice, you can omit ___.
Advertisement
Advertisement