Sobes.tech
Junior — Senior

The effect of calling append inside a function on the original slice

livecode

Task condition

It is necessary to understand how modifying the slice src using the append operation inside a function will affect the source array arr, which was used to create src.

package main

import "fmt"

func foo(src []int) {
    src = append(src, 5)
}

func main() {
    arr := []int{1, 2, 3}
    src := arr[:1]

    foo(src)

    fmt.Println(src)
    fmt.Println(arr)
}