Go – Slice Concat

In Go, slices are dynamic and allow flexible operations, including concatenation. Concatenating slices involves combining the elements of two or more slices into a single slice. This operation can be performed using the built-in append function and the variadic operator (...).

In this tutorial, we will learn how to concatenate slices in Go, covering different scenarios with practical examples and detailed explanations.


Syntax for Slice Concatenation

The syntax for concatenating slices is:

</>
Copy
resultSlice := append(slice1, slice2...)

Here:

  • slice1: The first slice to which elements are added.
  • slice2: The second slice whose elements are concatenated to slice1.
  • resultSlice: The resulting slice containing all elements of slice1 followed by all elements of slice2.
  • ...: The variadic operator, which expands slice2 into individual elements for appending.

Examples of Slice Concatenation

1. Concatenate Two Slices

This example demonstrates how to concatenate two slices:

</>
Copy
package main

import "fmt"

func main() {
    // Declare and initialize two slices
    slice1 := []int{1, 2, 3}
    slice2 := []int{4, 5, 6}

    // Concatenate the slices
    result := append(slice1, slice2...)

    // Print the resulting slice
    fmt.Println("Concatenated Slice:", result)
}

Explanation

  1. Declare Slices: Two slices slice1 and slice2 are initialized with values {1, 2, 3} and {4, 5, 6}, respectively.
  2. Concatenate Slices: The elements of slice2 are appended to slice1 using the append function with the variadic operator (...).
  3. Print Result: The resulting slice is printed using fmt.Println.

Output


2. Concatenate Multiple Slices

This example demonstrates how to concatenate multiple slices into a single slice:

</>
Copy
package main

import "fmt"

func main() {
    // Declare and initialize three slices
    slice1 := []int{1, 2}
    slice2 := []int{3, 4}
    slice3 := []int{5, 6}

    // Concatenate the slices
    result := append(slice1, append(slice2, slice3...)...)

    // Print the resulting slice
    fmt.Println("Concatenated Slice:", result)
}

Explanation

  1. Declare Slices: Three slices slice1, slice2, and slice3 are initialized with values {1, 2}, {3, 4}, and {5, 6}, respectively.
  2. Concatenate Slices: slice2 and slice3 are concatenated first, and their result is concatenated with slice1.
  3. Print Result: The resulting slice is printed using fmt.Println.

Output


3. Concatenate Slices of Strings

This example demonstrates how to concatenate slices of strings:

</>
Copy
package main

import "fmt"

func main() {
    // Declare and initialize two slices of strings
    slice1 := []string{"Hello", "World"}
    slice2 := []string{"from", "Go"}

    // Concatenate the slices
    result := append(slice1, slice2...)

    // Print the resulting slice
    fmt.Println("Concatenated Slice:", result)
}

Explanation

  1. Declare Slices: Two slices of strings slice1 and slice2 are initialized with values {"Hello", "World"} and {"from", "Go"}, respectively.
  2. Concatenate Slices: The elements of slice2 are appended to slice1 using the append function with the variadic operator (...).
  3. Print Result: The resulting slice is printed using fmt.Println.

Output


Points to Remember

  • Variadic Operator: The variadic operator (...) is required to expand a slice into individual elements when using append.
  • Dynamic Growth: Concatenating slices creates a new slice that can accommodate all the elements, possibly reallocating memory if necessary.
  • Type Consistency: All slices being concatenated must have the same element type.
  • Nested Concatenation: Multiple slices can be concatenated in a single operation by nesting append calls.