Length of Slice

To get length of slice in Go programming, call len() function and pass the slice as argument to it. len() function returns an integer, the length of slice.

The syntax to get the length of slice x is

len(x)

Return Value

The function returns an integer value, representing the length of given slice.

Example

In the following program, we take a slice in x, and find its length using len() function.

example.go

package main

import (
	"fmt"
)

func main() {
	var x = []int{5, 8, 14, 7, 3}
	var xLen = len(x)
	fmt.Print("Length of slice is : ", xLen)
}

Output

Length of slice is : 5
ADVERTISEMENT

Conclusion

In this Golang Tutorial, we learned how to get length of a slice in Go programming using len() function, with the help of example program.