How to get a part or section of an array in Go or Golang?
Tags: go
Published: 2022-09-02 18:32:43

---
  title: How to get a part or section of an array in Go or Golang?
  published: true
  tags: Go
  date: Sat Sep 03 2022 00:02:43 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/get-part-section-of-array-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-e6a9cb1d9ba726f62c4c8304d6be660a.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/get-part-section-of-array-go-golang)
  
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

```go
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]
}
```

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

```go
package main

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

**To know more about creating an array, see the blog on [How to create a fixed-size array in Go or Golang?](https://melvingeorge.me/blog/create-fixed-size-array-go-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,

```go
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]
}
```

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

```go
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]
}
```

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

```go
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]
}
```

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

See the above code live in [The Go Playground](https://go.dev/play/p/hJqTJBF7AVN).

That's all 😃!

### Feel free to share if you found this useful 😃.

---