Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Go access array elements

Shulou Source: shulou.com Published: 2022-06-01 02:31:34 09月12日 Update

In this article, the editor introduces in detail "how Go accesses array elements". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "Go how to access array elements" can help you solve your doubts.

Access array elements

Array elements can be read by index (position). The format is the array name followed by parentheses, and the value of the index is in the brackets. For example:

Var salary float32 = balance [9]

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

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

Example 1

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; iTunes + {

N [I] = I + 100 / * set the element to I + 100 * /

}

/ * output the value of each array element * /

For j = 0; j < 10; jacks + {

Fmt.Printf ("Element [% d] =% d\ n", j, n [j])

}

}

The execution result of the above example is as follows:

Element [0] = 100Element [1] = 101Element [2] = 102Element [3] = 103Element [4] = 104Element [5] = 105Element [6] = 106Element [7] = 107Element [8] = 108Element [9] = 109. at this point, the article "how to access array elements by Go" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, Welcome to the industry information channel.

Tags: Array element instance article content index square bracket proper position can be passed ideas new knowledge more format steps knowledge knowledge points articles details results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Huawei OPPO Reno vpn