Slices in Golang: Usage and examples

This article explains how to work with slices in Golang, including how to declare, access, and manipulate slices—a flexible way to handle arrays more efficiently in Go.

In Golang, slices are a special type of array that allows you to work with portions of an array without copying them. Slices are more flexible and easier to manage compared to traditional arrays, especially when dealing with dynamic data.

Golang Code

package main

import "fmt"

func main() {
    // Declare an initial array
    arr := [5]int{1, 2, 3, 4, 5}

    // Create a slice from the array
    slice := arr[1:4]

    // Print the elements of the slice
    fmt.Println("Slice:", slice)

    // Modify an element in the slice
    slice[0] = 10

    // Print the original array after modifying the slice
    fmt.Println("Original array:", arr)

    // Append an element to the slice using append
    slice = append(slice, 6)

    // Print the slice after appending
    fmt.Println("Slice after append:", slice)
}

Detailed explanation:

  1. arr := [5]int{1, 2, 3, 4, 5}: Creates an array with 5 elements.
  2. slice := arr[1:4]: Creates a slice from the array arr, taking elements from index 1 to 3 (excluding index 4).
  3. fmt.Println("Slice:", slice): Prints the elements of the slice.
  4. slice[0] = 10: Modifies the first element of the slice.
  5. fmt.Println("Original array:", arr): Prints the original array to show that the slice shares memory with the array.
  6. slice = append(slice, 6): Appends a new element to the slice using the append function.
  7. fmt.Println("Slice after append:", slice): Prints the slice after appending the new element.

Tips:

  • Slices share memory with the original array, so modifying the slice affects the array as well.
  • When using append, if the slice does not have enough memory, a new array will be created to store the data.
Tags: Golang, Slices


Related

How to Automatically Log in to a Website Using Selenium with Chrome in Golang

A guide on how to use Selenium in Golang to automatically log in to a website using the Chrome browser. This article provides specific code examples and detailed explanations of each step.
How to UPDATE data in a MySQL database using Golang

A guide on how to update data in a MySQL database using Golang with Prepared Statements involving multiple parameters for enhanced security and efficiency.
How to split a string in Golang using the SplitAfter function

A guide on how to use the `SplitAfter` function in Golang to split a string based on a specific character or substring. This article provides a detailed explanation of how the function works, along with examples.
JSON Web Token Authentication with Golang

A guide on how to implement JSON Web Token (JWT) authentication in a Golang application. This article details how to create, sign, and verify JWTs to secure an API.
How to open the Notepad application using Golang

A guide on how to use the `os/exec` package in Golang to open the Notepad application on Windows. This is a practical example of how to call and run external programs from a Go program.
Multithreading in Golang with Goroutine

A guide on how to handle multithreading in Golang using Goroutine, allowing efficient parallel processing and CPU optimization.
How to pass Authentication Header Token when POSTing data to an API using Golang

This guide explains how to pass an Authentication Header Token when making a POST request to an API using Golang. It covers handling HTTP requests, adding a token to the Header for authentication, and sending data to an API.
How to SELECT data from a MySQL database using Golang

A guide on how to use Golang to query data from a MySQL database using Prepared Statements with multiple parameters.
Send JavaScript code to a website using Golang and Selenium

A guide on how to use Selenium in Golang to send JavaScript code to a website on the Chrome browser. The article provides specific code and detailed explanations.
How to Split a String in Golang Using the SplitAfterN Function

A guide on how to use the `SplitAfterN` function in Golang to split a string based on a separator and limit the number of resulting parts. This function is useful when you need to split a string but retain the separator.

main.add_cart_success