Slices
So to solve the problem we had at hand in previous chapter, Slices are Go's response to have array of dynamic length. But there is gotcha, one need to define length of slice at the time of declaration, but more data of same type could be appended to slices in future. For example
So what we have done? We have defined a slice of type string having length 3. We can dynamically assign values to slices similar to array. In example above make
is similar to new keyword. Like
To get the length of slices, we have similar len
function as array
Go provides a special method called append
for slices
. this method actally creates a new slice with new lenght, assigning old and new element to it and returning a new slice. That returned slice should be saved in a variable to have access to it.
Unlike Array, in Go slices has copy
utility, that allows copying over slices. To copy our variable slice
to copied
variable, one can do like following
In first line we make another slice of length of slice
variable and then use copy
function to copy over content of slice
variable to copied
variable
In addition to all those utility methods, Go provide a slice operator for slices, syntax for that looks like [from:to]
where from
is index number of slice to start from and go upto to
index but excluding to
. To illustrate, we would use our previous example
So, now its more clear. This has sliced from index 2 to 4 (excluding index 5). Similarly
Go also allows special syntaxfrom start index to length of slice
Last updated