What is the purpose of the len and cap functions in Go when working with slices?

  • len returns the capacity of a slice, and cap returns the length.
  • len returns the length of a slice, and cap returns the capacity.
  • len returns the number of elements, and cap returns the address.
  • len and cap are not used with slices.
In Go, the len function returns the length (the number of elements) of a slice, while the cap function returns the capacity (the maximum number of elements the slice can hold without resizing). These functions are crucial when working with slices because they allow you to determine the current size of a slice and its capacity for future growth. Understanding how to use len and cap helps you manage and manipulate slices effectively in your Go programs.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *