Slices:
Slices
suggest changeSlice is a growable sequence of values of the same type.
Other languages call them arrays or vectors.
Memory used by slice is provided by a fixed size array. A slice is a view into that array.
Slice has length and capacity.
Capacity represents how many total elements a slice can have. That’s the size of underlying array.
Length is the current number of elements in the slice.
The difference between capacity and length is how many elements we can append to a slice before we have to re-allocate underlying array.
Zero value of a slice is nil.
Basic of slices
slice := make([]int, 0, 5)
// append element to end of slice
slice = append(slice, 5)
// append multiple elements to end
slice = append(slice, 3, 4)
fmt.Printf("length of slice is: %d\n", len(slice))
fmt.Printf("capacity of slice is: %d\n", cap(slice))
length of slice is: 3
capacity of slice is: 5
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
4
Strings
5
Pointers
6
Arrays
7
Slices
8
Maps
9
Structs
10
Interfaces
15
Functions
16
Methods
18
Defer
20
Concurrency
22
Mutex
23
Packages
27
Logging
30
JSON
31
XML
32
CSV
33
YAML
34
SQL
35
HTTP Client
36
HTTP Server
38
Reflection
39
Context
40
Package fmt
41
OS Signals
42
Testing
49
gob
50
Plugin
53
Console I/O
54
Cryptography
59
Contributors