Go/Arrays: Difference between revisions
From charlesreid1
No edit summary |
|||
| (26 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 | ||
= | =Basics= | ||
==Initialization== | |||
The most important thing to understand about arrays is that they are different from slices. | |||
If we create the variable like this: | |||
<pre> | |||
var keys [3]int | |||
</pre> | |||
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: | |||
<pre> | |||
var keys []int | |||
</pre> | |||
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> | |||
keys := [3]int{10,20,30} | |||
</pre> | |||
==Array size is a part of the type== | ==Array size is a part of the type== | ||
An important characteristic of arrays is that their size is a part of their 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: | The two variables defined here are of two distinct types: | ||
| Line 22: | Line 46: | ||
==Array slices== | ==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= | =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
| Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|