Sling Academy
Home/Golang/Passing a Slice to a Function as an Argument in Go

Passing a Slice to a Function as an Argument in Go

Last updated: November 26, 2024

In the Go programming language, slices are a versatile and commonly used data type that allow a program to store a sequence of elements. When working with slices, you might often need to pass them to functions as arguments to perform operations on their elements. This guide will show you how to do just that, with clear explanations and code examples.

Understanding Slices in Go

A slice in Go is a dynamically-sized, flexible view into the elements of an array. It’s essentially a reference to an underlying array with three properties: a pointer to the start of the slice, its length, and its capacity. One of the most powerful features of slices is that they can be resized as needed.

Declaring a Slice

Here’s a simple example of how to declare and initialize a slice in Go:

package main

import "fmt"

func main() {
    numbers := []int{1, 2, 3, 4, 5}
    fmt.Println(numbers)
}

When you run this code, it will output the contents of the slice numbers.

Passing a Slice to a Function

To pass a slice to a function, you simply include it as a parameter to the function. Here's an example:

package main

import "fmt"

func printSlice(nums []int) {
    for _, num := range nums {
        fmt.Println(num)
    }
}

func main() {
    numbers := []int{1, 2, 3, 4, 5}
    printSlice(numbers)
}

In this example, we define a function printSlice which takes a slice of integers as an argument and prints each element. Here’s what’s happening:

  • printSlice is defined with a parameter nums []int, indicating it takes a slice of integers.
  • The for range loop iterates over each element of the slice, printing it out.

Modifying a Slice Inside a Function

One of the advantages of passing slices by default in Go is that you can modify the contents of the slice within the function, and these changes will be reflected in calling function. Here's an example:

package main

import "fmt"

func modifySlice(nums []int) {
    for i := range nums {
        nums[i] *= 2
    }
}

func main() {
    numbers := []int{1, 2, 3, 4, 5}
    modifySlice(numbers)
    fmt.Println(numbers)
}

In this example, the modifySlice function doubles each element of the slice. As you run the main function, you will observe that the modifications inside modifySlice affect the numbers slice in main, resulting in the output:

[2 4 6 8 10]

Conclusion

Passing slices to functions in Go is both straightforward and efficient due to their reference-based nature. You can modify the contents of the slice within functions without copying the entire slice, which is particularly useful for large data sets. Practice using slices and passing them to functions to fully appreciate the flexibility they offer.

Next Article: Using Slices as Function Return Values in Go

Previous Article: Copying Slices in Go: Efficient Techniques

Series: Working with Slices in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant