In Go, an array is a numbered sequence of elements of a specific length. In typical Go code, slices are much more common; arrays are useful in some special scenarios. |
|
package main |
|
import "fmt" |
|
func main() { |
|
Here we create an array |
var a [5]int fmt.Println("emp:", a) |
We can set a value at an index using the
|
a[4] = 100 fmt.Println("set:", a) fmt.Println("get:", a[4]) |
The builtin |
fmt.Println("len:", len(a)) |
Use this syntax to declare and initialize an array in one line. |
b := [5]int{1, 2, 3, 4, 5} fmt.Println("dcl:", b) |
Array types are one-dimensional, but you can compose types to build multi-dimensional data structures. |
var twoD [2][3]int for i := 0; i < 2; i++ { for j := 0; j < 3; j++ { twoD[i][j] = i + j } } fmt.Println("2d: ", twoD) |
2 arrays are comparable |
var arr [5]int arr[4] = 100 arr2 := [5]int{0, 0, 0, 0, 100} fmt.Println("arr == arr2: ", arr == arr2) |
copy array re-assign array will be directly copied, and original arr will not be affected |
arr = [5]int{0, 0, 0, 0, 100} arr3 := arr arr3[4] = 200 fmt.Println("arr[4] = ", arr[4]) |
reverse array |
arr = [5]int{1, 2, 3, 4, 100} rev(&arr) // should pass array with refrence fmt.Println("reversed arr = ", arr) } |
func rev(arr *[5]int) { n := len(arr) for i := 0; i < n/2; i++ { arr[i], arr[n-1-i] = arr[n-1-i], arr[i] } } |
Note that arrays appear in the form |
$ go run arrays.go emp: [0 0 0 0 0] set: [0 0 0 0 100] get: 100 len: 5 dcl: [1 2 3 4 5] 2d: [[0 1 2] [1 2 3]] arr == arr2: true arr[4] = 100 reversed arr = [100 4 3 2 1] |
Next example: Slices.