Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on Array in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Comment

  1. Array in Golang is similar to Array in Rust in the sense that the length of an array is part of its type and must be determined at compile time.

  2. Array is a primitive (value) type in Golang. When assigned to another variable or passed as a parameter, it is copied! For this reason, array is not a good interface to use. Slice is prefer to array for function parameters.

In [14]:
import "reflect"

Construct Arrays

Create an integer array of 0's.

In [16]:
var arr[5]int
arr
Out[16]:
[0 0 0 0 0]
In [17]:
reflect.TypeOf(arr)
Out[17]:
[5]int

The length of an array is part of its type which means that it must be known at compile time. Specifying a (non-const) variable as the length of an array will the code fail to compile.

In [18]:
n := 10
var vals[n]int
vals
repl.go:2:10: array length is not a constant: [n]int

The value of a const variable is known at compile time, so can you specify a const integer variable as the length of an array.

In [19]:
const n = 10
var arr[n]int
arr
Out[19]:
[0 0 0 0 0 0 0 0 0 0]

Create a string array and explicitly specify the value at each index.

In [26]:
arr := [3]string{"how", "are", "you"}
arr
Out[26]:
[how are you]

Define an int array with inferred length. Note that ... must be used. If omitted, a slice instead of array is defined.

In [24]:
arr := [...]string{"how", "are", "you"}
arr
Out[24]:
[how are you]
In [25]:
reflect.TypeOf(arr)
Out[25]:
[3]string

Length of an Array

In [29]:
arr := [...]string{"how", "are", "you"}
arr
Out[29]:
[how are you]
In [30]:
len(arr)
Out[30]:
3

Indexing an Array

In [35]:
arr := [...]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
arr
Out[35]:
[0 10 20 30 40 50 60 70 80 90]
In [36]:
arr[0]
Out[36]:
0
In [37]:
arr[3]
Out[37]:
30
In [38]:
arr[3:7]
Out[38]:
[30 40 50 60]
In [39]:
reflect.TypeOf(arr[3:7])
Out[39]:
[]int

Loop Through an Array

The for ... range ... loop in Golang makes a copy of each value.

In [1]:
arr := [...]int{0, 1, 2}
arr
Out[1]:
[0 1 2]
In [2]:
for i, v := range arr {
    v *= 10
}
arr
Out[2]:
[0 1 2]
In [3]:
for i, v := range arr {
    arr[i] = v * 10
}
arr
Out[3]:
[0 10 20]

Slice

Slice in Golang is similar to Vec in Rust.

In [27]:
vec := []int{1, 2, 3}
vec
Out[27]:
[1 2 3]
In [28]:
reflect.TypeOf(vec)
Out[28]:
[]int

Create an integer slice with length 5 and capacity 5 using the function make.

In [4]:
s1 := make([]int, 5)
s1
Out[4]:
[0 0 0 0 0]
In [5]:
len(s1)
Out[5]:
5
In [6]:
cap(s1)
Out[6]:
5

Create an integer slice with length 5 and capacity 10 using the function make.

In [10]:
s2 := make([]int, 5, 10)
s2
Out[10]:
[0 0 0 0 0]
In [11]:
len(s2)
Out[11]:
5
In [12]:
cap(s2)
Out[12]:
10

Appending to a Slice

Appending to a slice

In [50]:
vec := []int{1, 2, 3}
vec
Out[50]:
[1 2 3]
In [51]:
vec = append(vec, 10, 20, 30)
vec
Out[51]:
[1 2 3 10 20 30]
In [52]:
v2 := []int{100, 200, 300, 400}
v2
Out[52]:
[100 200 300 400]
In [53]:
vec = append(vec, v2...)
vec
Out[53]:
[1 2 3 10 20 30 100 200 300 400]

Use Slice Instead of Array for Function Parameters

In [40]:
func updateSlice(s []int) {
    s[4] = 750
}
In [41]:
arr := [5]int{78, 89, 45, 56, 14}
arr
Out[41]:
[78 89 45 56 14]
In [44]:
updateSlice(arr[:])
arr
Out[44]:
[78 89 45 56 750]
In [47]:
vec := []int{78, 89, 45, 56, 14}
vec
Out[47]:
[78 89 45 56 14]
In [48]:
updateSlice(vec)
vec
Out[48]:
[78 89 45 56 750]

References

Go array

Comments