Logo

Programming-Idioms

History of Idiom 44 > diff from v27 to v28

Edit summary for version 28 by programming-idioms.org:
[Go] Emphasize in comment. +DocURL to append. +updated origin URL.

Version 27

2016-04-07, 14:26:44

Version 28

2016-09-27, 11:32:58

Idiom #44 Insert element in list

Insert element x at position i in list s. Further elements must be shifted to the right.

Idiom #44 Insert element in list

Insert element x at position i in list s. Further elements must be shifted to the right.

Code
s = append(s, 0)
copy(s[i+1:], s[i:])
s[i] = x
Code
s = append(s, 0)
copy(s[i+1:], s[i:])
s[i] = x
Comments bubble
Extend slice by 1 (it may trigger a copy of the underlying array).
Then shift elements to the right.
Then set s[i].
Comments bubble
Extend slice by 1 (it may trigger a copy of the underlying array).
Then shift elements to the right.
Then set s[i].
Doc URL
https://golang.org/pkg/builtin/#append
Origin
https://code.google.com/p/go-wiki/wiki/SliceTricks
Origin
https://github.com/golang/go/wiki/SliceTricks
Demo URL
http://play.golang.org/p/p2MRxxkyq1
Demo URL
http://play.golang.org/p/p2MRxxkyq1