[]
.[]T
defines a slice of type T.a
is an array below while s
is a slice.make
function. Make function accepts 3 parameters, first is type of slice, second is length of slice to create and optionally third parameter as capacity of slice.s := make([]T,length,capacity)
[]
with an index that begins at 0
similar to array access.make
all its elements get initialized to its zero values.make
function call when using slice literals.[]T{values}
array[startIndex:endIndex]
len(s) == 0
, and not s == nil
for checking for empty slice.for...range
==
unlike arrays. For most use cases where comparison needs to be done, we need to write our own comparison codeappend
function.append
can be used to add one or more elements to slice. Append operation may create new slice if new elements to add do not fit in existing capacity of slice. s := append(s, e)