Declaration of a one-dimensional array

Only the length of the array is declared during initialization

var linearArray [3]int println linearArray

If you don’t want to write the length of the array, you can use this method and let the compiler calculate the length of the array itself

linearArray1 := [...]int{1, 2, 3} println linearArray1

You can also declare the length of the array with the initial value

linearArray2 := [3]int{1, 2, 3} println linearArray2

Declaration of multidimensional arrays

You can declare a matrix with 2 rows and 2 columns like this

var matrix [2][2]int println matrix

If you need to declare more dimensions, you can extend them yourself, such as declaring a 2*2*3 three-dimensional array

var threeDimensionalArray [2][2][3]int println threeDimensionalArray


If you do not declare the contents of the array in Go+, the compiler automatically initializes the array to 0; For an array of type bool, the initial value is false.

For more array value types, Go+ supports strings, integers, floats, Booleans, etc. See this chapter for details:

Next example: Slices