Golang has "Slices" that are somewhat similar to arrays but with more flexible nature. Slices can be created in a couple of ways:
1: Using the make([]T, length, capacity)
function.
2: Using the format []T{elements}, where []T
is the type if the slice and elements are the elements of the slice.
For example to create a slice of integers called s
with a length of three elements, we can do the following:
s := make([]int ,3)
The first parameter of the make function holds the type of the slice you want to create, the second param is the length of the slice and the third param is the capacity of the slice.
Now, if you go ahead and print s, you will see an array of three elements, where all the elements have the value 0
:
package main
import "fmt"
func main() {
s := make([]int, 3)
fmt.Println(s)
}
Output:
[0 0 0 ]
OR if you know your elements ahead of time
package main
import "fmt"
func main() {
s := []int{3,32,2}
fmt.Println(s)
}
Output
[3 32 2]
Length of the slice
Calling the function len
returns the length of the slice. For instance, len(s)
returns 3 for the slice s.
The slice above can be populated using a couple of different ways:
package main
import "fmt"
func main() {
s := make([]int, 3)
s[0] = 10
s[1] = 20
s[2] = 30
fmt.Println(s)
}
The expected output of the above code is:
[10 20 30]
You can also use Golang's for
loop to populate the slice dynamically:
package main
import "fmt"
func main() {
s := make([]int, 3)
for i := range s {
s[i] = i + 10
}
fmt.Println(s)
}
Running the above code would output:
[10 11 12]
How about appending elements?
Golang has a built in function called append(slice, element)
. The function takes two parameters; the slice you want to append an element to and the element you are appending. Calling append
, however, does not modify the original slice, instead it returns a new slice so you have to make sure the it is stored in a variable so it may be used:
package main
import "fmt"
func main() {
s := make([]int, 3)
for i := range s {
s[i] = i + 10
}
fmt.Println("s = ", s)
x := append(s, 100)
fmt.Println("x = ", x)
}
The following will appear on the console as outputs where s is the original slice and x is the slice returned by append
:
s = [10 11 12]
x = [10 11 12 100]
Copying slices
We can also copy slice elements into another slice with the copy(copiedSlice, sliceToCopy)
package main
import "fmt"
func main() {
s := make([]int, 3)
for i := range s {
s[i] = i + 10
}
fmt.Println("s = ", s)
x := make([]int, 2)
copy(x, s)
fmt.Println("copied = ", x)
}
The following is the expected output where s is the origial slice and copied is the copy we created. Note, the copied slice has only 2 elements because we specified its length to be 2 when we first created it usng the make
function.
s = [10 11 12]
copied = [10 11]
This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond
This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond