Concatenate Two or More Slices in Go
In Go, slices are a powerful and flexible data structure for storing and manipulating collections of data. In this guide, we will explore several different methods for concatenating slices in Go, including using the built-in functions append()
and copy()
and a for
loop.
Using the append()
built-in function:
In this example, ...
is used to expand the second slice so it can be passed as separate arguments to the append function. The result is that the elements of slice2
are appended to the end of slice1
.
If you want to concatenate more than two slices then repeat the same step by appending the elements of slice3
to slice1
package main
import "fmt"
func main() {
// Declare the two slices
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
slice3 := []int{7, 8, 9}
// Using the append() built-in function
fmt.Println("Original slices:", slice1, slice2, slice3)
// concatenate two slices
slice1 = append(slice1, slice2...)
fmt.Println("Concatenated two slice:", slice1)
// concatenate two or more slices
slice1 = append(slice1, slice3...)
fmt.Println("Concatenated three slice:", slice1)
}
Output
Original slices: [1 2 3] [4 5 6] [7 8 9]
Concatenated two slice: [1 2 3 4 5 6]
Concatenated three slice: [1 2 3 4 5 6 7 8 9]
Using the copy()
built-in function:
In this example, we create a new slice concatenated
with a length that is the sum of the lengths of slice1
and slice2
. We then use the copy()
function to copy the elements of slice1
to the beginning of the new slice and the elements of slice2
to the end of the new slice.
package main
import "fmt"
func main() {
// Declare the two slices
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
fmt.Println("Original slices:", slice1, slice2)
// Create a new slice to store the concatenated slice
concatenated := make([]int, len(slice1)+len(slice2))
// Concatenate the slices using the copy function
copy(concatenated[:len(slice1)], slice1)
copy(concatenated[len(slice1):], slice2)
fmt.Println("Concatenated slice:", concatenated)
}
Output
Original slices: [1 2 3] [4 5 6]
Concatenated slice: [1 2 3 4 5 6]
You can use the same approach to concatenate two or more slices as shown below.
package main
import "fmt"
func main() {
// Declare the four slices
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
slice3 := []int{7, 8, 9}
// Create a new slice to store the concatenated slices
concatenated := make([]int, len(slice1)+len(slice2)+len(slice3))
// Copy the elements of slice1 to the new slice
copy(concatenated[:len(slice1)], slice1)
// Copy the elements of slice2 to the new slice after slice1
copy(concatenated[len(slice1):len(slice1)+len(slice2)], slice2)
// Copy the elements of slice3 to the new slice after slice2
copy(concatenated[len(slice1)+len(slice2):len(slice1)+len(slice2)+len(slice3)], slice3)
fmt.Println("Concatenated slice:", concatenated)
}
Output
Concatenated slice: [1 2 3 4 5 6 7 8 9]
Using a for
loop:
In this example, we first declare four slices slice1
, slice2
, slice3
, and slice4
. Then we create a new slice concatenated
with a length of 0. We then use a loop to iterate over the slices, and within the loop, we use another loop to iterate over the elements of each slice. We then use the append()
function to add each element to the new slice concatenated
.
package main
import "fmt"
func main() {
// Declare the four slices
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
slice3 := []int{7, 8, 9}
slice4 := []int{10, 11, 12}
// Create a new slice to store the concatenated slices
concatenated := make([]int, 0)
// Use a loop to iterate over the slices
slices := [][]int{slice1, slice2, slice3, slice4}
for _, s := range slices {
for _, value := range s {
concatenated = append(concatenated, value)
}
}
fmt.Println("Concatenated slice:", concatenated)
}
Output
Concatenated slice: [1 2 3 4 5 6 7 8 9 10 11 12]
Conclusion:
Concatenating slices in Go can be done in several ways. In this article, we explored the three common methods: using the built-in functions append()
and copy()
, and a for
loop.