Capacity and length of a slice in Go (Golang) – GOSAMPLES

Bạn đang tìm kiếm những thông tin mới nhất ? Hãy đọc bài viết này để cùng chúng tôi tìm hiểu về Golang cap Hãy cùng chúng tôi khám phá bài viết này

In Go, the size of a slice tells you what number of components it comprises. It may be obtained utilizing the len() perform. The capability is the dimensions of the slice’s underlying array and could be obtained with the cap() perform.

Distinction between arrays and slices

To higher perceive the distinction between the capability and size of a slice, first, it’s best to know the variations between arrays and slices.


An array is an listed assortment of a sure dimension with values of the identical sort, declared as:

Initializing an array


Properties of arrays

  • Arrays have a hard and fast dimension and can’t be resized. Slices could be resized.
  • The kind of the array consists of its dimension. The [4]int array sort is distinct from [5]int, they usually can’t be in contrast.
  • Initializing an array with var title [size]sort creates a set of dimension components of sort sort and every of them is the zero worth for the given sort.
  • Arrays are handed by worth. It implies that if you assign one array to a different, you’ll make a brand new copy of its contents:



A slice declared as:

is an information construction describing a bit of an array with three properties:

  • ptr – a pointer to the underlying array
  • len – size of the slice – variety of components within the slice
  • cap – capability of the slice – size of the underlying array, which can also be the utmost size the slice can take (till it grows)

A slice just isn’t an array. It describes a bit of the underlying array saved beneath the ptr pointer.

Initializing a slice


As we see within the output, var a []int creates a zero slice – a slice that has the size and capability equal to 0, and no underlying array.

Initializing a slice with the desired array, i.e., b := []int{0, 1, 2, 3}, creates a brand new slice with capability and size taken from the underlying array.

A slice may also be initialized with the built-in make() perform that takes the kind of a slice as the primary argument and the size because the second. The ensuing slice has a capability equals to the size, and the underlying array is initialized with zero values.

There may be additionally another model of the make() perform with three arguments: the primary is the kind of a slice, the second is the size, and the third is the capability. On this approach, you may create a slice with a capability higher than the size.

Properties of slices

  • A slice is routinely resized when the append() perform is named. Resizing right here implies that the append() provides new components to the top of the slice, and if there’s not adequate capability within the underlying array, a brand new array will likely be allotted. The append() perform all the time returns a brand new, up to date slice, so if you wish to resize a slice s it’s essential to retailer the lead to the identical variable s.
  • Slices should not comparable and easy equality comparability a == b just isn’t doable. See methods to examine slices.
  • Initializing a slice with var title []sort creates a zero slice that has size and capability equal to Zero and no underlying array. See what’s the distinction between nil and empty slices.
  • Similar to arrays (and every part in Go), slices are handed by worth. Once you assign a slice to a brand new variable, the ptr, len, and cap are copied, together with the ptr pointer that may level to the identical underlying array. When you modify the copied slice, you modify the identical shared array which makes all modifications seen within the previous and new slices:


Size and capability

You already know that capability is the dimensions of the slice’s underlying array and size is the variety of the slice components, however what’s the relationship between them? To grasp this higher, let’s analyze the re-slicing and appending operations.


Re-slicing is an operation that creates a brand new slice from an present one or an array. To “slice” an array or “re-slice” an present slice, use a half-open vary with two indices separated by a colon:


We get the identical outcomes for the slice:


Re-slicing a slice or an array creates a brand new slice with size given by indices vary and capability equal to the variety of components within the underlying array from the index of the primary component of the slice to the top of the array. See two extra examples of re-slicing operation – for vary with out the primary index s[:3], and with out the final index s[3:]:



The append() perform

Appending is among the most essential operations for slices. Since arrays in Go are immutable, solely with the append() perform we will get a variable-length information assortment. Nonetheless, as we all know, beneath slices nonetheless use arrays. The instance under exhibits what occurs when the variety of slice objects exceeds its capability.


As you may see within the output, each time the size of the slice is past its capability (the size of the underlying array), the append() perform expands the slice by allocating a brand new underlying array of twice its dimension and copying all of its components there. Discover that the pointer to the underlying array modifications with every change in capability.


To grasp the size and capability of slices in Go, it is very important perceive how slices work and what’s the distinction between slices and arrays. Slices are constructed on prime of arrays to offer variable-length information collections. They encompass three components – a pointer to the underlying array (beneath, slices use arrays as information storage), the size of the slice, and the capability – the dimensions of the underlying array. These Three properties are copied when a slice worth is handed, however the brand new pointer all the time factors to the identical shared array. The append() perform makes slices expandable, creating a robust and expressive information construction, one of the crucial utilized in Go.