Slices are a fundamental data structure in Go as they allow us to work with collection of elements.
One of the most common tasks when working with slices is concatenating two or more slices into a single unit.
In this tutorial, we will walk you through the methods that you can use to accomplish this task.
Golang Slices
Before we dive into the process of removing duplicates from a slice, let us explore the basics of slices in case you are not familiar.
What Is a Slice?
In Go, a slice is a dynamically-sized, flexible view into an underlying array. It is a more versatile alternative to arrays as it can grow or shrink as needed without requiring us to specify a fixed size.
We can use slices to represent the sequences of data such as lists, collections, or arrays with dynamic sizes.
A slice is comprised of three main components in Go:
- Pointer to the underlying array
- Length (number of elements in the slice)
- Capacity (maximum number of elements that the slice can hold without resizing the underlying array)
The following shows the basic syntax of declaring a slice in Go:
Basics of Slices in Go
The first and most common method of creating a slice in Go is using the literal definition as shown in the following example:
We can also use the “make” function to declare a new slice as demonstrated in the following example:
This should create a slice of integers with the length of 3 and a capacity of 5.
We can also create a slice by slicing an existing array as follows:
slice := arr[1:4]
This should create a new slice from the second and fourth elements of the array.
Adding Elements to a Slice
In Go, we can add the elements to an existing slice using the append() function as shown in the following example:
databases = append(databases, "Redis")
In the “append” function, we need to pass the existing slice and the new elements that we wish to add to the slice.
Accessing the Elements
We can access the slice elements using the elements index as shown in the following example:
This should return the first element in the slice.
Iterating Over Slice
We can use a basic “for” loop to iterate over the elements of a slice as shown in the following example:
fmt.Printf("Index: %d, Value: %s\n", i, db)
}
Go Slice Concatenation
Luckily, we have several methods and techniques that we can use to concatenate the slices in Go. Let us look at them:
Method 1: Using the Append Function
This one of the most common methods of appending the items to a slice. We can also use it to append the slices into another slices. The method also returns the resulting slice which makes it quite easy and efficient.
An example is as follows:
import "fmt"
func main() {
sql := []string{"MySQL", "PostgreSQL", "SQLite"}
nosql := []string{"MongoDB", "Cassandra", "Redis"}
databases := append(sql, nosql...)
fmt.Println(databases)
}
The given example uses the “…” operator which is known as slice unpacking. Once we unpack the elements of the slice, we pass them to the append() function to add them to the slice.
This is a very efficient and simple methods of slice concatenation in Go.
Method 2: Copying the Elements
However, can we forget about copy? We can use a copy functionality to copy the elements from the elements and add them to a new slice as shown in the following example:
import "fmt"
func main() {
sql := []string{"MySQL", "PostgreSQL", "SQLite"}
nosql := []string{"MongoDB", "Cassandra", "Redis"}
totalLen := len(sql) + len(nosql)
databases := make([]string, totalLen)
copy(databases, sql)
copy(databases[len(sql):], nosql)
fmt.Println(databases)
}
In the given example, we start by calculating the total length of the concatenated string. We then create a new slice with the required capacity.
Finally, we copy the elements of the first and second slices into the new slice.
Conclusion
In this post, we explored how to concatenate the slices into a single unit which is a common task when dealing with collections.