- Learn Data Structures and Algorithms with Golang
- Bhagvan Kommadi
- 125字
- 2021-06-24 15:37:38
Slice function
Slices are passed by referring to functions. Big slices can be passed to functions without impacting performance. Passing a slice as a reference to a function is demonstrated in the code as follows (slices.go):
//twiceValue method given slice of int type
func twiceValue(slice []int) {
var i int
var value int
for i, value = range slice {
slice[i] = 2*value
}
}
// main method
func main() {
var slice = []int{1,3,5,6}
twiceValue(slice)
var i int
for i=0; i< len(slice); i++ {
fmt.Println(“new slice value”, slice[i])
}
}
Run the following command:
go run slices.go
The following screenshot displays the output:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/2e58fe8f-0186-4866-9e9d-b0e255611dae.png?sign=1739025510-OIB00cFrj8SfTbew9osqj1GmlJAmcJZb-0-be41f52eb831d14b0d2fce859aeed367)
Now that we know what slices are, let's move on to two-dimensional slices in the next section.