How to get the total number of elements that an array can hold or the capacity of an array in Go or Golang?
Tags: go
Published: 2022-09-13 22:09:26

---
  title: How to get the total number of elements that an array can hold or the capacity of an array in Go or Golang?
  published: true
  tags: Go
  date: Wed Sep 14 2022 03:39:26 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/get-total-number-of-elements-array-can-hold-or-capacity-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-98085e5ab77d8c739e2216b77657d521.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/get-total-number-of-elements-array-can-hold-or-capacity-go-golang)
  
To get the total number of elements an array can hold or its capacity in Go or Golang, you can use the `cap()` built-in function and then pass the array or the array variable as an argument to the function.

The method returns an `int` type value that refers to the total capacity of the array.

### TL;DR

```go
package main

import "fmt"

func main() {
    // a simple array
    // that can hold `10` elements.
    // Add 2 elements to the array.
    names := [10]string{"John Doe", "Lily Roy"}

    // get the total capacity of the `names`
    // array using the `cap()` built-in function
    totalCapacity := cap(names)

    // log to the console
    fmt.Println(totalCapacity) // 10 ✅
}
```

For example, let's say we have a `string` type array called `names` that can hold `10` elements like this,

```go
package main

func main() {
    // a simple array
    // that can hold `10` elements
    names := [10]string{}
}
```

Let's also add 2 elements to the array like this,

```go
package main

func main() {
    // a simple array
    // that can hold `10` elements.
    // Add 2 elements to the array.
    names := [10]string{"John Doe", "Lily Roy"}
}
```

Now to get the total capacity of the `names` array, we can use the `cap()` built-in function and pass the `names` array as an argument to it like this,

```go
package main

func main() {
    // a simple array
    // that can hold `10` elements.
    // Add 2 elements to the array.
    names := [10]string{"John Doe", "Lily Roy"}

    // get the total capacity of the `names`
    // array using the `cap()` built-in function
    totalCapacity := cap(names)
}
```

Finally, let's print the `totalCapacity` value to the console like this,

```go
package main

import "fmt"

func main() {
    // a simple array
    // that can hold `10` elements.
    // Add 2 elements to the array.
    names := [10]string{"John Doe", "Lily Roy"}

    // get the total capacity of the `names`
    // array using the `cap()` built-in function
    totalCapacity := cap(names)

    // log to the console
    fmt.Println(totalCapacity) // 10 ✅
}
```

As you can see that the value of `10` is printed on the console.

**NOTE: The capacity of an array should not be confused with the length of an array. The length of an array refers to the total number of elements currently present in the array.**

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

That's all 😃!

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

---