find the length of an array in go

To find the length of an array in Go, you can use the built-in len function. Here's an example:

main.go
package main

import "fmt"

func main() {
    numbers := [5]int{2, 4, 6, 8, 10}
    length := len(numbers)
    fmt.Println(length) // Output: 5
}
146 chars
10 lines

In this example, we declare an array of int called numbers with 5 elements. We then use the len function to find the length of the array and assign that value to a variable called length. Finally, we print the value of length.

The len function can also be used on slices, maps, and strings in Go.

related categories

gistlibby LogSnag