WEB DEVELOPER SITE
htmLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Arrays


The Go language provides an array type data structure.

A array is a sequence of numbered and fixed-length data items of the same unique type, which can be any primitive type such as integer, string, or custom type.

Relative to the variable declaring number0, number1, ..., number99, using the array form numbers[0], numbers[1] ..., numbers[99]< /strong> More convenient and easy to expand.

Array elements can be read (or modified) by index (position), starting with 0, with the first element indexed as 0, the second index as 1, and so on.

Declare array

Go language array declarations need to specify the element type and the number of elements, the syntax is as follows:

var variable_name [SIZE]variable_type

The above is how a one-dimensional array is defined. For example, the following defines an array with a length of 10 and a type of float32:

var balance [10] float32

Initialize array

The following demonstrates array initialization:

var balance = [5]float32{1000.0 , 2.0, 3.4, 7.0, 50.0}

The number of elements in {} in the initialization array cannot be greater than the number in [].

If you ignore the number in [] without setting the array size, Go will set the size of the array based on the number of elements:

 var balance = [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}

This instance is the same as the above example, although the size of the array is not set.

 balance[4] = 50.0

The above example reads the fifth element. Array elements can be read (or modified) by index (position), starting with 0, the first element index is 0, the second index is 1, and so on.


Access array elements

Array elements can be read by index (location). The format is the array name followed by brackets, and the values in the brackets are the index. For example:

var salary float32 = balance[9 ]

The above example reads the value of the 10th element of the array balance.

The following demonstrates an example of an array complete operation (declaration, assignment, access):

Instance

package main

import "fmt"

func main() {
   var n [10]int /* n Is an array of length 10 */
   var i,j int

   /* Initialize elements for array n */        
   for i = 0; i < 10; i++ {
      n[i] = i + 100 /* Set the element toi + 100 */
   }

   /* Output the value of each array element */
   for j = 0; j < 10; j++ {
      fmt.Printf("Element[%d] = %d\n", j, n[j] )
   }
}

The above example execution results are as follows:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

Go Arrays in Detail

Sr.No Concept & Description
1 Multi-dimensional arrays Go language supports multidimensional arrays, the simplest multidimensional array is a two-dimensional array

2 Passing arrays to functions

You can pass array argument to the function

Accessing Two-Dimensional Array Elements

An element in dimensional array is accessed with the aid of using the subscripts, i.E., row index and column index of the array. For instance −

int val = a[2][3]