How to pass a Slice to Function in Golang?

Slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. It is just like an array having an index value and length, but the size of the slice is resized they are not in fixed-size just like an array. In Go language, you are allowed to pass a slice to a function, means the function gets the copy of the slice.
The slice is passed by value to a function along with the slice capacity, length, and the pointer of the slice is always pointing to the underlying array. So, if we made some change in the slice which is passed to the function by value will reflect in the slice present outside the function. Let us discuss this concept with the help of an example:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to
// pass a slice to the function
package main
  
import "fmt"
  
// Function in which slice
// is passed by value
func myfun(element []string) {
  
    // Modifying the given slice
    element[2] = "Java"
    fmt.Println("Modified slice: ", element)
}
  
// Main function
func main() {
  
    // Creating slice
    slc := []string{"C#", "Python", "C", "Perl"}
      
    fmt.Println("Initial slice: ", slc)
  
    // Passing the slice to the function
    myfun(slc)
      
    fmt.Println("Final slice:", slc)
  
}

chevron_right


Output:



Initial slice:  [C# Python C Perl]
Modified slice:  [C# Python Java Perl]
Final slice: [C# Python Java Perl]

Explanation: In the above example, we have a slice named as slc. This slice is passed in the myfun() function. As we know that the slice pointer always points to the same reference even if they passed in a function. So, when we change the value C to Java present at index value 2. This change reflects the slice present outside the function too, so the final slice after modification is [C# Python Java perl].

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to
// pass a slice to the function
package main
  
import "fmt"
  
// Function in which slice
// is passed by value
func myfun(element []string) {
  
    // Here we only modify the slice
    // Using append function
    // Here, this function only modifies
    // the copy of the slice present in 
    // the function not the original slice
    element = append(element, "Java")
    fmt.Println("Modified slice: ", element)
}
  
// Main function
func main() {
  
    // Creating a slice
    slc := []string{"C#", "Python", "C", "Perl"}
      
    fmt.Println("Initial slice: ", slc)
  
    // Passing the slice
    // to the function
      
    myfun(slc)
    fmt.Println("Final slice: ", slc)
  
}

chevron_right


Output:

Initial slice:  [C# Python C Perl]
Modified slice:  [C# Python C Perl Java]
Final slice:  [C# Python C Perl]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.