Sobes.tech
Junior — Senior

The effect of the append function on a slice passed by value

livecode

Task condition

Determine what output the program will produce when a slice is passed to a function, and append is applied to it inside the function. Note that in Go, slices are passed by value, so changes made to the slice inside the function may not affect the original slice declared outside the function.

func main() {
    testSlice := make([]string, 0, 3)
    testSlice = append(testSlice, "Hello")
    testSlice = append(testSlice, "Hello")
    test(testSlice)

    fmt.Println(testSlice)
}

func test(testSlice []string) {
    testSlice = append(testSlice, "Bye")
}