Table of contents
We already know how to declare and work with variables, constants and pointers, however you won't be working to long before you are going to run into a situation where you need to associate a group of related things together, and to do that, we need collections.
Collection types of Go
There are four types that Go offers for us:
- Array
- Slice
- Map
- Struct
Some higher-level constructs are available and you will find in the standard library and other libraries like linked list but those are not included in the language itself and so we are going to focus on the built-in collection types
Arrays
Arrays are fixed sized collection of similar data types. We can have an array of integers, array of strings etc.
var arr [3]int
arr[0] = 1
arr[1] = 2
arr[2] = 3
Now one thing you should avoid when you work with arrays, is to over index them. So in this case you should not print the arr[3] value.
There is one another option to declare an array by the following way
arr2 := [3]int{1,2,3}
This is a much more elegant way to declare an array.
Slices
Slices are built on top of arrays. Lets copy the values from the
func main() {
arr2 := [3]int{1,2,3}
slice := arr2[:]
fmt.Println(arr2,slice)
}
Here the : operators tell that to copy all the elements of the array from the begining to the end.
Now if you change the code to this
func main() {
arr2 := [3]int{1,2,3}
slice := arr2[:]
arr2[1] = 42
slice[2] = 27
fmt.Println(arr2,slice)
}
You can see that the values are 1,42,27 in both slice and arr2. How? arr2 is a physical three-element array sitting in memory and its values are directly related to the array itself. Now slice is kind of a pointer. It's not really a pointer but its pointing to that underlying array. So any changes on the slice or the array will be visible vice-versa.
So Slices are like dynamic arrays, but how can we append items to it? We use the append method
func main() {
slice := []int{1,2,3}
fmt.Println(slice)
slice = append(slice, 4)
fmt.Println(slice)
}
This way we appended the value 4 to the slice.
We can not only make slices of arrays. We can make slices of slices too (sub-slices)
func main() {
slice := []int{1,2,3}
fmt.Println(slice)
slice = append(slice, 4)
fmt.Println(slice)
s2 := slice[1:]
s3 := slice[:2]
s4 := slice[1:2]
fmt.Println(s2,s3,s4)
}
Map
Maps are collection types that associate arbitrary keys with the values that we are storing in the collection.
An example of a map
func main() {
m := map[string]int{"foo":42}
fmt.Println(m)
fmt.Println(m["foo"])
m["foo"] = 27
fmt.Println(m)
delete(m,"foo")
fmt.Println(m)
}
To create a map you have to use the map keyword. Maps are not read only so you can change the value of it, and you can delete a value from a map with the delete keyword.
Struct
It is unique in the collections that Go provides for us in a fat that it is the only collection type that allows us to associate disparate data types together. So in array and slice all the elements are the same type. In maps, all the keys and the values have to be the same type. So those data types are homogenous. The two possible ways to declare and use a struct can be seen below
func main() {
type user struct {
ID int
FirstName string
LastName string
}
var u user
u.ID = 1
u.FirstName = "Arthur"
u.LastName = "Lastname"
fmt.Println(u)
u2 := user{ID:1,
FirstName: "Arthur2",
LastName: "Lastname2",
}
fmt.Println(u2)
}
Did you find this article valuable?
Support Renátó Bogár by becoming a sponsor. Any amount is appreciated!