02_update-slice-in-function

command
v0.0.0-...-84d5eb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2022 License: MIT Imports: 1 Imported by: 0

README

Passinng a slice to a function

func main() {
    nums := []int{1, 2, 3}
    add10(nums)
    fmt.Println(num)
    // output 11, 12, 13

    nums = []int{1, 2, 3}
    add3Items(nums)
    fmt.Println(num)
    // output
    // 1, 2, 3
}
case 1
func add10(vals []int) {
    for i := 0; i < len(vals); i++ {
        vals[i] += 10
    }
}

slice type is passwed by value to add10. However, within the function slice vals refers to the same underlying array

therefore any update on slice without changing the underlying array will be reflected outside the function. Hence, add10 will have its effect persisted even after add10 is completes execution.

case 2
func add3Items(vals []int) {
	for i := 100; i <= 300; i += 100 {
		vals = append(vals, i)
	}
}

If the underlying array has to change (recreated/replaced) within the function the changes will not be available outside the function. This is because outside the function the slice will be still refering to the old array as the underlying type.

In other words, withing the function if cap or len changes, if the underlying array has to expand to accomodate a new value the change will not be reflected.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL