Sobes.tech
Junior — Senior

Adjusting the behavior of slices in Go

livecode

Task condition

It is necessary to modify the given example so that the function foo changes the slice passed to it, and the result of the append call is preserved after the function exits.

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) // [1 5]
 fmt.Println(arr) // [1 2 3] — arr is not changed because src is a slice of it
}