Go Program to Merge Two Slices

Introduction

Merging two slices in Go involves appending the elements of one slice to another. This operation combines both slices into a single slice containing all the elements. This guide will demonstrate how to merge two slices in Go.

Problem Statement

Create a Go program that:

  • Declares and initializes two slices.
  • Merges the two slices into a single slice.
  • Displays the merged slice.

Example:

  • Input: Slice 1: [1, 2, 3], Slice 2: [4, 5, 6]
  • Output: Merged slice: [1, 2, 3, 4, 5, 6]

Solution Steps

  1. Import the fmt Package: Use import "fmt" for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Create and Initialize the Slices: Use slice literals to create and initialize two slices.
  4. Merge the Slices: Use the append function to merge the two slices into a single slice.
  5. Display the Merged Slice: Use fmt.Println to display the merged slice.

Go Program

package main

import "fmt"

/**
 * Go Program to Merge Two Slices
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Create and initialize the first slice
    slice1 := []int{1, 2, 3}
    fmt.Println("Slice 1:", slice1)

    // Step 2: Create and initialize the second slice
    slice2 := []int{4, 5, 6}
    fmt.Println("Slice 2:", slice2)

    // Step 3: Merge the two slices using the append function
    mergedSlice := append(slice1, slice2...)

    // Step 4: Display the merged slice
    fmt.Println("Merged slice:", mergedSlice)
}

Explanation

Step 1: Create and Initialize the First Slice

  • The first slice is created and initialized using a slice literal. For example, slice1 := []int{1, 2, 3} creates a slice of integers with three elements.

Step 2: Create and Initialize the Second Slice

  • The second slice is created similarly to the first one. For example, slice2 := []int{4, 5, 6} creates another slice of integers.

Step 3: Merge the Two Slices

  • The append function is used to merge the two slices. The syntax append(slice1, slice2...) appends all elements of slice2 to slice1. The ... is the unpacking operator, which allows all elements of slice2 to be passed as individual arguments to append.

Step 4: Display the Merged Slice

  • The program prints the merged slice using fmt.Println.

Output Example

Example 1:

Slice 1: [1 2 3]
Slice 2: [4 5 6]
Merged slice: [1 2 3 4 5 6]

Example 2:

If you merge slices with different lengths:

slice1 := []int{10, 20, 30}
slice2 := []int{40, 50}
mergedSlice := append(slice1, slice2...)
fmt.Println("Merged slice:", mergedSlice)

Output:

Merged slice: [10 20 30 40 50]

Example 3:

If you merge slices of strings:

slice1 := []string{"Hello", "World"}
slice2 := []string{"Go", "Lang"}
mergedSlice := append(slice1, slice2...)
fmt.Println("Merged slice:", mergedSlice)

Output:

Merged slice: [Hello World Go Lang]

Conclusion

This Go program demonstrates how to merge two slices using the append function. It covers basic programming concepts such as slice manipulation and how to dynamically combine slices in Go. This example is useful for beginners learning Go programming and understanding how to work with and manipulate slices effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top