Menu

You Appended Each Parcel's Label to the Shared List and Every Parcel Got the Last One
📰

You Appended Each Parcel's Label to the Shared List and Every Parcel Got the Last One

DEV Community: go·Serguey Asael Shinder·12 days ago
#7CkIhvBi
#devtogo#devto#dev#append#common
Long article (312 words) — no AI summary yet.
Reading 0:00
15s threshold

The despatch service

built one list

of labels every parcel needed.

Read from config,

one append at a time.

Then, for each parcel,

it added that parcel's own label.

var common []string

for _, l := range cfg.Labels {

common = append(common, l)

}

for i := range parcels {

parcels[i].Labels = append(common, parcels[i].Extra)

}

For two years

the config had two labels.

Fragile. This way up.

Then someone added a third.

Keep dry.

The next morning

every parcel in the batch

carried the same extra label.

The one meant

for the last parcel.

Here is the mechanism.

A Go slice

is a small header:

a pointer to an array,

a length,

a capacity.

append writes

into the spare capacity

if there is any.

It only copies

to a new array

when there is no room.

Built one append at a time,

the array behind common

grew to 1, then 2, then 4.

Two labels,

capacity two.

No room,

so every append

made a fresh copy.

The bug was there.

It just could not fire.

Three labels,

capacity four.

One free…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.