Sobes.tech
Junior — Senior

How the append function changes slices and references

livecode

Task condition

It is necessary to predict what data will be output by the program, considering the features of slices, pointers, and possible copying during append calls.

 type account struct {
     value int
 }

 func main() {
     listA := make([]account, 0, 2)
     listA = append(listA, account{})
     listA = append(listA, account{})
     listB := append(listA, account{})

     ptr := &listB[0]
     ptr.value = 100

     fmt.Println(listA, listB)

     listA = append(listB, account{})
     ptr.value += 100

     fmt.Println(listA, listB)
 }