Sobes.tech
Junior — Senior

Slice behavior when using append in Go

livecode

Task condition

Determine what result will be printed by the program below. It is necessary to find out whether the second slice changes after modifications to the first — that is, whether they share the same underlying array or if a separate copy is created.

package main

import "fmt"

func main() {
    var sliceA []int
    var sliceB []int

    sliceA = append(sliceA, 1)
    sliceA = append(sliceA, 2)
    sliceA = append(sliceA, 3)
    sliceB = append(sliceA, 4)
    sliceA = append(sliceA, 5)

    fmt.Println(sliceA, sliceB) // ?
}