DEV Community

Cover image for How to get a part or section of an array in Go or Golang?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to get a part or section of an array in Go or Golang?

#go

Originally posted here!

To get a part or section of an array, you have to use the concept of Slices in Go or Golang. Slices in simple terms are nothing but a view of the section of an array and are formed by specifying the start and end indexes of that array.

To make a slice from an array, we can specify the start and end indexes of an array separated by the : symbol (colon) inside an opening and a closing square brackets symbol ([]).

TL;DR

package main

import "fmt"

func main() {
    // an array of names
    names := [4]string{"John Doe", "Lily Roy", "Daniel Doe", "Embla Marina"}

    // get the array values from the index `0`
    // till the index of `2` using a slice
    // and providing the `start` value as `0`
    // and end value as `3`
    namesSlice := names[0:3]

    // log the contents of `namesSlice` to console
    fmt.Println(namesSlice) // βœ… [John Doe Lily Roy Daniel Doe]
}
Enter fullscreen mode Exit fullscreen mode

For example, let's say we have an array of names like this,

package main

func main(){
    // an array of names
    names := [4]string{"John Doe", "Lily Roy", "Daniel Doe", "Embla Marina"}
}
Enter fullscreen mode Exit fullscreen mode

To know more about creating an array, see the blog on How to create a fixed-size array in Go or Golang?

Now if we want to get a part of an array or all the values of the array from the index of 0 to the index of 2. To do that we have to use the concept of Slices in Go.

Let's first start the slice by writing a [] symbol and inside the brackets we can first write the starting index which is the value of 0 followed by the : symbol and then by writing the ending index of 3. You might doubt why we had put the value of 3 instead of 2. In the slices, to get the value in the ending index which is the 2 and index we have to write the value of 3 so that while picking the items it will iterate till the 3 rd index but doesn't include the value in the resulting slice.

It can be done like this,

package main

func main(){
    // an array of names
    names := [4]string{"John Doe", "Lily Roy", "Daniel Doe", "Embla Marina"}

    // get the array values from the index `0`
    // till the index of `2` using a slice
    // and providing the `start` value as `0`
    // and end value as `3`
    names[0:3]
}
Enter fullscreen mode Exit fullscreen mode

Let's now assign the slice to a new variable called namesSlice like this,

package main

func main(){
    // an array of names
    names := [4]string{"John Doe", "Lily Roy", "Daniel Doe", "Embla Marina"}

    // get the array values from the index `0`
    // till the index of `2` using a slice
    // and providing the `start` value as `0`
    // and end value as `3`
    namesSlice := names[0:3]
}
Enter fullscreen mode Exit fullscreen mode

Finally, we can log the contents of the namesSlice to the console like this,

package main

import "fmt"

func main() {
    // an array of names
    names := [4]string{"John Doe", "Lily Roy", "Daniel Doe", "Embla Marina"}

    // get the array values from the index `0`
    // till the index of `2` using a slice
    // and providing the `start` value as `0`
    // and end value as `3`
    namesSlice := names[0:3]

    // log the contents of `namesSlice` to console
    fmt.Println(namesSlice) // βœ… [John Doe Lily Roy Daniel Doe]
}
Enter fullscreen mode Exit fullscreen mode

As you can see from the above code the namesSlice only contains the value from the 0th index to the index of 2 which is what we want.

See the above code live in The Go Playground.

That's all πŸ˜ƒ!

Feel free to share if you found this useful πŸ˜ƒ.


Top comments (0)