Junior — Middle
What components does a slice structure include in Go?
sobes.tech AI
Answer from AI
In the Go language, a slice structure includes three main components:
- Pointer to the array — the address of the first element of the slice within the underlying array.
- Length (len) — the number of elements available in the slice.
- Capacity (cap) — the maximum number of elements the slice can hold, starting from the current pointer.
Slices provide a convenient way to work with dynamic sequences, using these three parameters to manage access and expansion without the need to copy data.
Example of creating a slice:
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4] // pointer to arr[1], len=3, cap=4