<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Go%2FArrays%2FArray_Slice_Insertion</id>
	<title>Go/Arrays/Array Slice Insertion - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Go%2FArrays%2FArray_Slice_Insertion"/>
	<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Go/Arrays/Array_Slice_Insertion&amp;action=history"/>
	<updated>2026-06-19T18:03:16Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Go/Arrays/Array_Slice_Insertion&amp;diff=25765&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;{{Main|Go/Arrays}}  The Go blog (this post: https://blog.golang.org/slices) gets lost on a (rather stupid and uninformative) topic, which is, array slices.   To insert an elem...&quot;</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Go/Arrays/Array_Slice_Insertion&amp;diff=25765&amp;oldid=prev"/>
		<updated>2018-12-13T00:07:26Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{Main|Go/Arrays}}  The Go blog (this post: https://blog.golang.org/slices) gets lost on a (rather stupid and uninformative) topic, which is, array slices.   To insert an elem...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Main|Go/Arrays}}&lt;br /&gt;
&lt;br /&gt;
The Go blog (this post: https://blog.golang.org/slices) gets lost on a (rather stupid and uninformative) topic, which is, array slices. &lt;br /&gt;
&lt;br /&gt;
To insert an element into a slice of an array, we can do the following (NOTE: This is NOT the same as inserting an element into an array):&lt;br /&gt;
&lt;br /&gt;
* Extend the slice window by 1&lt;br /&gt;
* Shift the upper part of the slice&amp;#039;s underlying array data up by 1, using the copy function&lt;br /&gt;
** Copying FROM the variable slice, starting at index and running to the end&lt;br /&gt;
** Copying TO the variable slice, starting at index + 1 and running to the end&lt;br /&gt;
* Return the new slice&lt;br /&gt;
&lt;br /&gt;
This operation WILL NOT affect the underlying data.&lt;br /&gt;
&lt;br /&gt;
Here is the Insert function, plus a main method that calls it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Insert inserts the value into the slice at the specified index,&lt;br /&gt;
// which must be in range.&lt;br /&gt;
// The slice must have room for the new element.&lt;br /&gt;
func Insert(slice []int, index, value int) []int {&lt;br /&gt;
&lt;br /&gt;
    // Grow the slice by one element.&lt;br /&gt;
    slice = slice[0 : len(slice)+1]&lt;br /&gt;
&lt;br /&gt;
    // Use copy to move the upper part of the slice out of the way and open a hole.&lt;br /&gt;
    copy(slice[index+1:], slice[index:])&lt;br /&gt;
&lt;br /&gt;
    // Store the new value.&lt;br /&gt;
    slice[index] = value&lt;br /&gt;
&lt;br /&gt;
    // Return the result.&lt;br /&gt;
    return slice&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aside from the stupidity of the example (not making clear why you would want to insert something into a slice of an array but not the array itself; not making clear the underlying data or the memory/reference model in Go).&lt;br /&gt;
&lt;br /&gt;
Basically this whole blog post is a hot mess.&lt;br /&gt;
&lt;br /&gt;
The confusion here is, the text seems to contradict the behavior of this function.&lt;br /&gt;
&lt;br /&gt;
* The text describing this example insists we are passing a copy of the slice, but that copy should refer to the same data underneath.&lt;br /&gt;
* However, when we expand the length of the slice, and shift everything over, and insert a value, this doesn&amp;#039;t affect the data underneath.&lt;br /&gt;
&lt;br /&gt;
The answer: we aren&amp;#039;t creating a slice by saying &amp;lt;code&amp;gt;slice = orig[5:10]&amp;lt;/code&amp;gt;, we are creating a slice by saying &amp;lt;code&amp;gt;slice = make([]int, 4, 8)&amp;lt;/code&amp;gt; and then &amp;lt;code&amp;gt;copy(slice,orig)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When we call make, we&amp;#039;re basically allocating an underlying array for a slice.&lt;br /&gt;
&lt;br /&gt;
[https://stackoverflow.com/questions/9320862/why-would-i-make-or-new Related question on Stack Overflow]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{GoFlag}}&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>