golang tutorial - Golang Slice | Go slicing - golang - go programming language - google go - go language - go program - google language
- Go Slice is an abstraction over Go Array.
- To overcome the Go Array limitations, slicing option comes into the picture. It allows to define type of variables that can hold several data items of the same kind but it do not provide any inbuilt method to increase size of it dynamically or get a sub-array of its own.
- It provides many utility functions required for an Array and is widely used in Go programming.
Defining a slice
len() and cap() functions
- Slice is an abstraction over array. It actually uses array as an underlying structure.
- len() function returns the number of elements presents in the slice
- cap() function returns the max capacity of slice as how many elements it can be accommodate
- When the above code is compiled and executed, it produces the following result:
- If a slice is declared with no inputs the by default, it is initialized as nil. Its length and capacity are zero
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output for the above go programming code
- Slice allows lower-bound and upper bound to be specified to get the subslice of it using[lower-bound:upper-bound].
append() and copy() functions
- Slice allows increasing the capacity of a slice using append() function.Using copy() function, contents of a source slice are copied to destination slice