["one" "two" "another"]
"one"
"two"
and "another"
. Since it has 3 elements, the length of the array is 3. The first element "one"
has the index 0
while the last element "another"
has index 2
. We will explore the importance of indices shortly.interface{}
interface{}
as a special data type which allow storage of any data-type of our choice thus dealing with the strict single-type limitations of slices and maps.templates.Slice
.String
method which can be used on a user datatype viz. .User.String
is actually a method that extracts the string notation for a user datatype and outputs it.cslice
Syntax : cslice <elements> ...
cslice
is a function used to create/define a new Slice (slice of datatype templates.Slice). It accepts the elements of slice as argument. As discussed earlier the elements can be of any datatype.
Example :
{{$new_slice := cslice 1 2 "new" 4.5 .User}}
The above snippet creates a new slice with 5 elements as specified above and stores in a variable $new_slice
.\len
Syntax : len <composite datatype or string>
len
is a function which can be used to find the length of any composite datatype (and even strings) including templates.Slice.
Example :
{{$new_slice := cslice 1 2 "new" 4.5 .User}}{{len $new_slice}}
len
function finds the length of the $new_slice
and outputs it producing the output 5
(since it has 5 different elements).\index
Syntax : index <composite datatype or string> <index>
\index
is a function which can be used to index or fetch elements of any composite datatype (and even indexing individual bytes in a String) including templates.Slice. As discussed earlier, the starting position of a slice is 0
and goes upto length - 1
. It accepts the composite datatype to index as first argument and the index to fetch as second and outputs the value of the element.
Example :
{{$new_slice := cslice 1 2 "new" 4.5 .User}}{{index $new_slice 2}}
In the above snippet, a new slice $new_slice
is created as discussed above. Then the index
function is used to fetch the third element (since index
starts at 0, 2 corresponds to third element) of $new_Slice
and outputs it producing new
as output.\.Append
Syntax : <Slice>.Append <element>
Appending refers to adding a single element to the end. We often want to grow Slices by adding and new element to the end of a Slice. For this, we use the .Append
method. It is important to note that .Append
method is only defined for a _templates.Slice _and not any generic slice. Append method outputs/returns a new slice with the added element but does not modify the original slice. It accepts one argument which is the element to be added.
Example :
{{$new_slice := cslice 1 2}}{{$appended_slice := $new_slice.Append 3.5}}
{{$new_slice}}
{{$appended_slice}}
Above code snippet produces the output :
[1 2]
[1 2 3.5]
Explanation: As discussed above, initially a Slice $new_slice
is created with cslice
. Then .Append
method is used on $new_slice
with an argument 3.5
. This produces a new slice with the additional element 3.5
which is stored in $appended_slice
. Finally the $new_slice
and $appended_slice
are output. Notice how $new_slice
remains unchanged.\.AppendSlice
Syntax : <Slice>.AppendSlice <other_slice>
Sometimes we want to join or merge two Slices by adding all elements of one Slice to the end of another Slice. .AppendSlice
method enables us to do that. Just like .Append
, .AppendSlice
method can only be invoked on a templates.Slice and produces a new Slice as output without modifying the original Slice or the slice being merged. It is important to note that the second slice to be joined can be in fact any generic slice and need not be a _templates.Slice. _It accepts the slice to be merged as an argument.
Example :
{{$slice1 := cslice 1 2}}{{$slice2 := cslice "one" "two" "three"}}{{$slice3 := $slice1.AppendSlice $slice2}}
{{$slice1}}
{{$slice2}}
{{$slice3}}
Output :
[1 2]
["one" "two" "three"]
[1 2 "one" "two" "three"]
Explanation :
Initially 2 slices, $slice1
and $slice2
were created using cslice
function. Then .AppendSlice
method is invoked on $slice1
with $slice2
as argument thus producing a new slice with elements of $slice2
added to the end of $slice1
and stored in $slice3
. Then all 3 of the slices are output. Note how neither the Slice on which the method is invoked nor the Slice being merged are altered rather producing a third slice which is a merge of the two Slices. \slice
:
Syntax : slice <slice or string> <start> <stop (optional)>
The slice
function can be used to produce sub-slices (as well as sub-strings!). What it means is that we can take a portion of a slice (say element number 3 to element number 6) and form a new slice out of it. It can work on any generic slice (and not just templates.Slice) and the newly produced slice retains the datatype of the parent slice. It accepts two or three arguments. The first argument is the slice (or string!) to produce a sub-slice from, the second being the start index and optionally a third stop index. If the stop index is not provided, it goes upto the end of the slice/string. Also note the indices specified are start <= sub-slice/string < stop
. So if we want a sub-slice from index 2 to index 5 (that is element number 3 to element number 6), start should be 2 but stop should be 6.
Example :
{{$new_slice := cslice 1 2 3 4 5 6 7 8}}{{slice $new_slice 2 6}}
In the above snippet, initially $new_slice
containing 8 elements is created using cslice
function. Say we wanted to make a sub-slice using element number 3(index 2) upto element number 6 (index 5). This is what the above snippet does. Hence, the arguments to slice
function are start = 2
and stop = 6
(since stop must be one greater than the index of last element).
Note: Do not confuse between a slice datatype and the slice
function. Although they are represented by the same word, the slice datatype is an ordered collection of indexable elements while the slice
function is used to form a sub-slice/string from a given slice/string. As you also might have guessed, this function can be used to remove the first or last elements from a slice as well (as well as middle elements if used with .AppendSlice
).\.
is modified and attains the value of successive elements of the slice.\.
is still given the successive elements of the slice. However, the variable $value
also attains successive elements of the slice.
Case 3:.
is still given the successive elements of the slice. However, the variable $value
also attains successive elements of the slice while the variable $index
is passed the value of the successive indices of the slice (from 0
to length -1
).\.StringSlice
Syntax : <slice>.StringSlice <strict-flag (optional)>
This is a unique method which can be used to extract and return all string (or string representable) elements of a template.Slice in form of a string slice (type []string). If none are string an empty []string slice is returned. It has an optional strict flag. f strict-flag is set to _true _it will return a []string only if all elements are pure string, else <no value>
is returned. This is also useful in a few cases where certain functions or methods only accept string slices and not a templates.Slice.
Example :
{{$slice := cslice "This" 1 "Is" "Fun"}}{{$new := $slice.StringSlice}}
{{joinStr "-" $new}}
Output :
This-Is-Fun
In the above snippet, initially a Slice is created with cslice
called $slice
. Then on invoking .StringSlice
method on $slice
, a new slice of type []string containing only the string datatype elements of $slice is created and stored in $new
. Then we using joinStr
function to join the elements of the []string slice into a single string with -
as separator and the resultant string is output.seq
function in performing a repeated task a known number of times. However, did you know that seq actually also produces a slice. It is of type _[]int _and contains integers greater than equal to the first argument (start) upto one less than the second argument (stop).