Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Passing slices to functions in Go (Golang) is quite common and idiomatic. Since slices are reference types, when you pass a slice to a function, you're actually passing a reference to the underlying data. Therefore, if the slice is modified inside the function, those modifications will be visible outside the function as well.
Here's a tutorial on how to pass a slice to a function in Go:
package main import "fmt" // A function that accepts a slice of integers func modifySlice(s []int) { for i := range s { s[i] = i * i // Modify the slice elements } fmt.Println("Inside modifySlice:", s) } func main() { slice := []int{1, 2, 3, 4, 5} modifySlice(slice) fmt.Println("Inside main:", slice) // The changes made inside modifySlice are reflected here }
Keep in mind that if you append to a slice inside a function, it may create a new underlying array for the slice, especially if the original slice's capacity is exceeded. In such cases, the modifications may not reflect outside the function.
package main import "fmt" // A function that appends to the slice func appendToSlice(s []int) []int { s = append(s, 6, 7, 8) // Appending to the slice fmt.Println("Inside appendToSlice:", s) return s } func main() { slice := []int{1, 2, 3, 4, 5} slice = appendToSlice(slice) fmt.Println("Inside main:", slice) // Reflects the appended values }
Remember, slices have a pointer to the underlying array, length, and capacity. When you pass a slice to a function, you're passing the slice header (which includes the pointer). You can change the slice's length and capacity within the function, but if you reassign the slice variable itself, it won't affect the original slice.
package main import "fmt" // A function that tries to modify the slice header func modifySliceHeader(s []int) { s = s[:2] // Changing the length of the slice fmt.Println("Inside modifySliceHeader:", s) } func main() { slice := []int{1, 2, 3, 4, 5} modifySliceHeader(slice) fmt.Println("Inside main:", slice) // The change in length inside modifySliceHeader isn't reflected here }
Slices in Go are reference types. When you pass them to functions, you're passing a reference to the underlying data, which means modifications inside the function are usually reflected outside.
However, be mindful of situations where you're dealing with the slice's header or appending to it. Depending on the operations, changes may or may not reflect outside the function. If unsure, you can always return the modified slice from the function and reassign it outside.
Golang pass slice to function example:
Description: Passing a slice to a function in Golang.
Code:
package main import "fmt" func printSlice(slice []int) { for _, value := range slice { fmt.Println(value) } } func main() { mySlice := []int{1, 2, 3, 4, 5} printSlice(mySlice) }
Passing slices as parameters in Golang functions:
Description: Passing slices as parameters to Golang functions.
Code:
package main import "fmt" func modifySlice(slice []int) { for i := range slice { slice[i] *= 2 } } func main() { mySlice := []int{1, 2, 3} modifySlice(mySlice) fmt.Println("Modified Slice:", mySlice) }
How to pass a slice by reference in Golang:
Description: Passing a slice by reference to a Golang function.
Code:
package main import "fmt" func modifySlice(slice *[]int) { for i := range *slice { (*slice)[i] *= 2 } } func main() { mySlice := []int{1, 2, 3} modifySlice(&mySlice) fmt.Println("Modified Slice:", mySlice) }
Working with slices as function arguments in Golang:
Description: Demonstrating how to work with slices as function arguments in Golang.
Code:
package main import "fmt" func processSlice(slice []int) int { sum := 0 for _, value := range slice { sum += value } return sum } func main() { mySlice := []int{1, 2, 3, 4, 5} result := processSlice(mySlice) fmt.Println("Sum of Slice:", result) }
Golang slice passing by reference or value:
Description: Understanding whether Golang slices are passed by reference or value.
Code:
package main import "fmt" func modifySlice(slice []int) { slice[0] = 100 } func main() { mySlice := []int{1, 2, 3} modifySlice(mySlice) fmt.Println("Original Slice:", mySlice) }
Passing slices of different types to functions in Golang:
Description: Passing slices of different types to functions in Golang.
Code:
package main import "fmt" func printStrings(slice []string) { for _, value := range slice { fmt.Println(value) } } func main() { stringSlice := []string{"one", "two", "three"} printStrings(stringSlice) }
Passing slices vs arrays to functions in Golang:
Description: Comparing the differences between passing slices and arrays to functions in Golang.
Code:
package main import "fmt" func modifySlice(slice []int) { for i := range slice { slice[i] *= 2 } } func modifyArray(arr [3]int) { arr[0] = 100 } func main() { mySlice := []int{1, 2, 3} modifySlice(mySlice) fmt.Println("Modified Slice:", mySlice) myArray := [3]int{1, 2, 3} modifyArray(myArray) fmt.Println("Original Array:", myArray) }
Using pointers to pass slices to functions in Golang:
Description: Utilizing pointers to pass slices to functions in Golang.
Code:
package main import "fmt" func modifySlice(slice *[]int) { for i := range *slice { (*slice)[i] *= 2 } } func main() { mySlice := []int{1, 2, 3} modifySlice(&mySlice) fmt.Println("Modified Slice:", mySlice) }