Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the array of Go language

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use the array of Go language". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Definition of array variables

Let's try to declare only the type, not the initial value. At this point, the compiler assigns "zero" to the array by default. The zero value of the array is the zero value of all internal elements.

Package main

Import "fmt"

Func main () {

Var a [9] int

Fmt.Println (a)

}

-

[0 0 0]

Let's take a look at the other three forms of variable definition, and the effect is the same.

Package main

Import "fmt"

Func main () {

Var a = [9] int {1,2,3,4,5,6,7,8,9}

Var b [10] int = [10] int {1,2,3,4,5,6,7,8,9,10}

C: = [8] int {1,2,3,4,5,6,7,8}

Fmt.Println (a)

Fmt.Println (b)

Fmt.Println (c)

}

-

[1 2 3 4 5 6 7 8 9]

[1 2 3 4 5 6 7 8 9 10]

[1 2 3 4 5 6 7 8]

Access to the array

Next, let's use the subscript to simply manipulate the array, which stores the square of the number.

Package main

Import "fmt"

Func main () {

Var squares [9] int

For I: = 0; I < len (squares); iTunes + {

Squares [I] = (I + 1) * (I + 1)

}

Fmt.Println (squares)

}

-

[1 49 16 25 36 49 64 81]

Subscript out-of-bounds checking of arrays (high-level knowledge)

We noticed in the above code that you can use the built-in function len () to get the length of the array directly. The length of the array is determined at compile time, and when we use the len () function to access the length property of the array, the compiler secretly replaces it with an integer value behind it.

Package main

Import "fmt"

Func main () {

Var a = [5] int {1 Jing 2 Jing 3 Jing 4 Jing 5}

A [101] = 255

Fmt.Println (a)

}

-

. / main.go:7:3: invalid array index 101 (out of bounds for 5-element array)

The running result of the above code shows that the Go language performs a compiler check on array access subscripts out of bounds. An important question is, if the subscript is a variable, how does Go check that the subscript is out of bounds? Variables need to be run-time to decide whether or not to cross the bounds. How does Go do that?

Package main

Import "fmt"

Func main () {

Var a = [5] int {1 Jing 2 Jing 3 Jing 4 Jing 5}

Var b = 101

A [b] = 255

Fmt.Println (a)

}

-

Panic: runtime error: index out of range

Goroutine 1 [running]:

Main.main ()

/ Users/qianwp/go/src/github.com/pyloque/practice/main.go:8 + 0x3d

Exit status 2

The answer is that Go inserts subscript out-of-bounds checking logic into the compiled code, so the subscript access efficiency of the array is not as efficient as the array access performance of the C language.

Array assignment

Only arrays of the same subelement type and the same length can be assigned to each other, otherwise they are different array types and cannot be assigned. The assignment of an array is essentially a shallow copy operation, and the values of the two array variables assigned are not shared.

Package main

Import "fmt"

Func main () {

Var a = [9] int {1,2,3,4,5,6,7,8,9}

Var b [9] int

B = a

A [0] = 12345

Fmt.Println (a)

Fmt.Println (b)

}

-

[12345 2345 6 7 8 9]

[1 2 3 4 5 6 7 8 9]

From the running results of the above code, you can see that the two arrays do not share internal elements after the assignment. If the length of the array is very large, then the copy operation has some overhead, and you must be careful when using it. What happens if we try to assign values with different lengths of arrays?

Package main

Import "fmt"

Func main () {

Var a = [9] int {1,2,3,4,5,6,7,8,9}

Var b [10] int

B = a

Fmt.Println (b)

}

-

. / main.go:8:4: cannot use a (type [9] int) as type [10] int in assignment

You can see that assignment between arrays of different lengths is prohibited because they belong to different types.

Traversal of array

In addition to traversing arrays using subscripts, you can also use the range keyword to traverse. Range traversal provides the following two forms.

Package main

Import "fmt"

Func main () {

Var a = [5] int {1 Jing 2 Jing 3 Jing 4 Jing 5}

For index: = range a {

Fmt.Println (index, a [index])

}

For index, value: = range a {

Fmt.Println (index, value)

}

}

-

0 1

1 2

2 3

3 4

4 5

0 1

1 2

2 3

3 4

4 5

This is the end of the content of "how to use arrays in Go language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report