Syntax
In Go, the length is also a part of array type. So the following code declares an array:
var array [3]int
while “var slice []int” defines a slice. Arrays cannot be resized.
Another way is to set the array entries as you declare the array:
func main() {
a := [2]string{"hello", "world!"}
fmt.Printf("%q", a)
}
You can also use an implicit length when passing values to array using [...]:
func main() {
a := [...]string{"hello", "world!"}
fmt.Printf("%q", a)
}
Printing arrays
There are multiple ways to print arrays, using fmt package. Eg.
func main() {
a := [2]string{"hello", "world!"}
fmt.Println(a) // [hello world!]
fmt.Printf("%s\n", a) // [hello world!]
fmt.Printf("%q\n", a) // ["hello" "world!"]
}
Multi-dimensional arrays
You can also create multi-dimensional arrays:
func main() {
var a [2][3]string
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
a[i][j] = fmt.Sprintf("row %d - column %d", i+1, j+1)
}
}
fmt.Printf("%q", a)
// [["row 1 - column 1" "row 1 - column 2" "row 1 - column 3"]
// ["row 2 - column 1" "row 2 - column 2" "row 2 - column 3"]]
}
Gotchas
Arrays of different length cannot be assigned
Because of this characteristic, arrays with the same array element type but different length can’t assign values each other. I.E.:
package main
import "fmt"
func main() {
var a1 [2]int
var a2 [3]int
a2 = a1
fmt.Println(a2)
}
The compiler will complain:
cannot use a1 (type [2]int) as type [3]int in assignment
Changing “var a1 [2]int” to “var a1 [3]int” will make it work.
Keep clear between slice and array
Another caveat you should pay attention to is the following code declares an array, not a slice:
array := [...]int {1, 2, 3}
You can verify it by the following code:
package main
import (
"fmt"
"reflect"
)
func main() {
array := [...]int {1, 2, 3}
slice := []int{1, 2, 3}
fmt.Println(reflect.TypeOf(array), reflect.TypeOf(slice))
}
The output is:
[3]int []int
Arrays are pass by value
Additionally, since in Go, the function argument is passed by “value”, so if you use an array as a function argument, the function just does the operations on the copy of the original copy. Check the following code:
package main
import (
"fmt"
)
func changeArray(array [3]int) {
for i, _ := range array {
array[i] = 1
}
fmt.Printf("In changeArray function, array address is %p, value is %v\n", &array, array)
}
func main() {
var array [3]int
fmt.Printf("Original array address is %p, value is %v\n", &array, array)
changeArray(array)
fmt.Printf("Changed array address is %p, value is %v\n", &array, array)
}
The output is:
Original array address is 0xc082008680, value is [0 0 0]
In changeArray function, array address is 0xc082008700, value is [1 1 1]
Changed array address is 0xc082008680, value is [0 0 0]
From the log, you can see the array’s address in changeArray function is not the same with array’s address in main function, so the content of original array will definitely not be modified. Furthermore, if the array is very large, copying them when passing argument to function may generate more overhead than you want, you should know about it.
References
https://github.com/NanXiao/golang-101-hacks http://www.golangbootcamp.com/book/collection_types