Overview
Clone an order struct in Go so the copy's items can change without touching the original: assigning a struct copies a slice field's header, not its items.
- assigning a struct copies its fields
- a slice field still shares its items
- copy the slice for a deep copy
Short Course
Assigning a struct copies its fields, but a slice field is a small header
pointing to shared items. Trap: after c := o, changing c.Items[0] changes
o.Items[0] too. Copy the slice itself.
c := o // copies ID, shares Items
c.Items[0] = "cap" // o.Items[0] is "cap" too
c.Items = append([]string{}, o.Items...)
Task
Write Clone(o Order) Order: a copy whose Items can change without touching
the original. Example: changing the first item of the clone leaves o.Items
as it was.
Test Suites
Pending
Next
Output