Go Program to Append Elements to a Slice

Introduction

In Go, slices are dynamically sized, which means you can append elements to a slice even after it has been created. The append function is used to add elements to a slice, and it automatically adjusts the size of the slice as needed. This guide will demonstrate how to append elements to a slice in Go.

Problem Statement

Create a Go program that:

  • Declares and initializes a slice.
  • Appends new elements to the slice.
  • Displays the slice before and after appending elements.

Example:

  • Input: Initial slice: [1, 2, 3], Elements to append: 4, 5, 6
  • Output: Slice after appending elements: [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 Slice: Use a slice literal to create and initialize a slice.
  4. Append Elements to the Slice: Use the append function to add new elements to the slice.
  5. Display the Slice: Use fmt.Println to display the slice before and after appending elements.

Go Program

package main

import "fmt"

/**
 * Go Program to Append Elements to a Slice
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Create and initialize a slice using a slice literal
    slice := []int{1, 2, 3}
    fmt.Println("Initial slice:", slice)

    // Step 2: Append a single element to the slice
    slice = append(slice, 4)
    fmt.Println("Slice after appending 4:", slice)

    // Step 3: Append multiple elements to the slice
    slice = append(slice, 5, 6)
    fmt.Println("Slice after appending 5 and 6:", slice)

    // Step 4: Append elements from another slice
    moreElements := []int{7, 8, 9}
    slice = append(slice, moreElements...)
    fmt.Println("Slice after appending elements from another slice:", slice)
}

Explanation

Step 1: Create and Initialize a Slice Using a Slice Literal

  • A slice can be created and initialized directly using a slice literal. For example, slice := []int{1, 2, 3} creates a slice of integers with three elements.

Step 2: Append a Single Element to the Slice

  • The append function is used to add a single element to the slice. For example, slice = append(slice, 4) appends the element 4 to the slice.

Step 3: Append Multiple Elements to the Slice

  • The append function can also be used to add multiple elements to the slice at once. For example, slice = append(slice, 5, 6) appends the elements 5 and 6 to the slice.

Step 4: Append Elements from Another Slice

  • You can append elements from one slice to another using the append function with the ... operator. For example, slice = append(slice, moreElements...) appends all elements from the moreElements slice to the original slice.

Step 5: Display the Slice

  • The program prints the slice using fmt.Println to display its elements before and after appending new elements.

Output Example

Example 1:

Initial slice: [1 2 3]
Slice after appending 4: [1 2 3 4]
Slice after appending 5 and 6: [1 2 3 4 5 6]
Slice after appending elements from another slice: [1 2 3 4 5 6 7 8 9]

Conclusion

This Go program demonstrates how to append elements to a slice using the append function. It covers basic programming concepts such as slice manipulation, appending single and multiple elements, and merging slices in Go. This example is useful for beginners learning Go programming and understanding how to dynamically manage and modify slices.

Leave a Comment

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

Scroll to Top