From charlesreid1

 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
Go blog post: arrays, slices, and strings: https://blog.golang.org/slices
Go blog post: arrays, slices, and strings: https://blog.golang.org/slices


=Arrays in Go=
=Basics=


"Arrays are not often seen in Go programs because the size of an array is part of its type, which limits its expressive power."
==Initialization==
 
The most common use of arrays are to store slices, which we will see in a moment.


==Array size is a part of the type==
The most important thing to understand about arrays is that they are different from slices.


An important characteristic of arrays is that their size is a part of their type.
If we create the variable like this:
 
The two variables defined here are of two distinct types:


<pre>
<pre>
var buffer [256]byte
var keys [3]int
var buffer2 [512]byte
</pre>
</pre>


This is because the size of the array is allocated at initialization time. You can use the square bracket syntax to access elements of an array, <code>buffer[0]</code> through <code>buffer[255]</code>. The program crashes if you access an index outside of its range.
it is an array type. That's because we've specified the precise capacity that keys should have.
 
==Array slices==


The bracket notation with colons can be used to refer to a slice of an array. For example:
But if we create the variable like this:


<pre>
<pre>
var buffer [256]byte
var keys []int
...
var slice []byte = buffer[100:150]
</pre>
</pre>


alternatively,
it is a slice type. That's because we did not specify the capacity.
 
In both cases, we have not actually initialized the value of either variable. Here's how we create an array:


<pre>
<pre>
slice := buffer[100:150]
keys := [3]int{10,20,30}
</pre>
</pre>


Think of a slice variable as a data structure with two elements: a length, and a pointer to an element of the array.
==Array size is a part of the type==


We can also take a slice of the slice:
An important characteristic of arrays is that their size is a part of their type.


<pre>
"Arrays are not often seen in Go programs because the size of an array is part of its type, which limits its expressive power."
slice2 := slice[5:10]
</pre>


Here's a shortcut to drop the first and last elements:
The two variables defined here are of two distinct types:


<pre>
<pre>
slice3 := slice[1:len(slice)-1]
var buffer [256]byte
var buffer2 [512]byte
</pre>
</pre>


===When is underlying slice modified===
This is because the size of the array is allocated at initialization time. You can use the square bracket syntax to access elements of an array, <code>buffer[0]</code> through <code>buffer[255]</code>. The program crashes if you access an index outside of its range.


Consider this simple function:
==Array slices==


<pre>
See [[Go/Slices]]
func AddOneToEachElement(slice []byte) {
    for i := range slice {
        slice[i]++
    }
}


func main() {
==Comparison==
    slice := buffer[10:20]
    for i := 0; i < len(slice); i++ {
        slice[i] = byte(i)
    }
    fmt.Println("before", slice)
    AddOneToEachElement(slice)
    fmt.Println("after", slice)
}
</pre>


Again, this requires us to think about the array slice as a data structure, which contains a pointer to an array and a length. The bundle, the data structure, is not a pointer itself!
Let's cover how to compare arrays.


Even though the slice (also called the "slice header") is passed by value, the header includes a pointer to elements of an array. Thus, both the original and the copy passed to the function refer to the same underlying array (slots in memory).
Array values are comparable if values of the array element type are comparable.  


===When is the slice actually a copy===
Two array values are equal if their corresponding elements are equal.


The argument to the function is a copy:
However, note that you cannot compare slices: https://play.golang.org/p/Kk8osjPm8n


<pre>
Likewise: https://play.golang.org/p/kCVoPekPudc
func SubtractOneFromLength(slice []byte) []byte {
    slice = slice[0 : len(slice)-1]
    return slice
}
 
func main() {
    fmt.Println("Before: len(slice) =", len(slice))
    newSlice := SubtractOneFromLength(slice)
    fmt.Println("After:  len(slice) =", len(slice))
    fmt.Println("After:  len(newSlice) =", len(newSlice))
}
</pre>


=Flags=
=Flags=


{{GoFlag}}
{{GoFlag}}

Latest revision as of 18:09, 14 December 2018

Go blog post: arrays, slices, and strings: https://blog.golang.org/slices

Basics

Initialization

The most important thing to understand about arrays is that they are different from slices.

If we create the variable like this:

var keys [3]int

it is an array type. That's because we've specified the precise capacity that keys should have.

But if we create the variable like this:

var keys []int

it is a slice type. That's because we did not specify the capacity.

In both cases, we have not actually initialized the value of either variable. Here's how we create an array:

keys := [3]int{10,20,30}

Array size is a part of the type

An important characteristic of arrays is that their size is a part of their type.

"Arrays are not often seen in Go programs because the size of an array is part of its type, which limits its expressive power."

The two variables defined here are of two distinct types:

var buffer [256]byte
var buffer2 [512]byte

This is because the size of the array is allocated at initialization time. You can use the square bracket syntax to access elements of an array, buffer[0] through buffer[255]. The program crashes if you access an index outside of its range.

Array slices

See Go/Slices

Comparison

Let's cover how to compare arrays.

Array values are comparable if values of the array element type are comparable.

Two array values are equal if their corresponding elements are equal.

However, note that you cannot compare slices: https://play.golang.org/p/Kk8osjPm8n

Likewise: https://play.golang.org/p/kCVoPekPudc

Flags