You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
line1: rear := result[id:]
line2: front := result[:id]
line3: temp := append(front, item)
line4: result = append(temp, rear...)
the bug of "append" is : before it execute the line 3, the rear equals to [-4, 2, 2], after it execute the line 4, the rear equals to [-3, 1, 3] for no reason which is really weird
What did you expect to see?
the expect result is : after it execute the line3, the variable rear won't change. and the codes ' purpose is to insert a item
What did you see instead?
before it execute the line 3, the rear equals to [-4, 2, 2], after it execute the line 3, the rear equals to [-3, 1, 3] for no reason which is really weird.
The text was updated successfully, but these errors were encountered:
That looks to be how append is supposed to work (not a bug) and is documented because it reuses the slices backing array up to capacity.
A copy for rear can be made if rear should not change due to other slices operations modifying the same backing array. Alternatively for this very specific code the capacity can be limited so append creates a new backing array for temp by using front := result[:id:id].
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
not sure
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
play.golang.org link is below:
https://play.golang.org/p/SAooamwMWuc
line1: rear := result[id:]
line2: front := result[:id]
line3: temp := append(front, item)
line4: result = append(temp, rear...)
the bug of "append" is : before it execute the line 3, the rear equals to [-4, 2, 2], after it execute the line 4, the rear equals to [-3, 1, 3] for no reason which is really weird
What did you expect to see?
the expect result is : after it execute the line3, the variable rear won't change. and the codes ' purpose is to insert a item
What did you see instead?
before it execute the line 3, the rear equals to [-4, 2, 2], after it execute the line 3, the rear equals to [-3, 1, 3] for no reason which is really weird.
The text was updated successfully, but these errors were encountered: