The following content is all the content of B station. If you don’t like to read articles and prefer to watch videos, you can click the link to enter B station to watch.

www.bilibili.com/video/BV1bR…

In order to save you time, none of the videos are controlled within 8 minutes.

An array of

What is our array? For example, if we had elements like 5,8, 9,79,76, how would we use go if we hadn’t learned the idea of an array?

x:= 5
y:=8
z:=9
q:=79
t:=76
Copy the code

And that’s when we think about is there a variable that can represent all of these elements at once? Hence the idea of an array.

An array is a collection of elements of the same data type. Elements in an array cannot be mixed with different elements (such as integers and strings).

The statement

Var Name of a variable [length] Data type var a [3]intCopy the code

Var a [3]int Declares an array of integers of length 3. So that’s one way we can define an array.

Some concepts of arrays

Initialization of an array

When we buy a new phone, it comes with a bunch of default Settings when it’s turned on. Arrays also have default values, if we don’t set them, default values for int, float are 0, string is an empty string, and bool is false

Var a = [10] int,2,3,4,5,6,7,8,10 {1} : b = [10] int,2,3,4,5,6,7,8,9 {1} / / is that if you do not fill out here... What you get is a slice of Go. If you fill in the array of go... The length array a:=[...] is generated from the following elements. ,2,3,4,5,6,7,8,9 {1}Copy the code

So those are the three ways we can define an array.

Through the array

For I:=0; i<len(a); For index, value:= range a{FMT. Println (index, value)}Copy the code

If only convenient, do not deal with the elements in the array, such as LeeCode array to do the most double pointer problem, usually use method two can be developed. More readable.

I in method one is our subscript.

Index and value in method 2 correspond to subscripts and elements in the array, respectively.

Multidimensional array

Multidimensional arrays can be understood in terms of two dimensions, and three dimensions, and usually we use two-dimensional arrays a lot.

We can also understand that a two-dimensional array is a table. It has the form of a row and a column

A table like this one we can use

Var a [2][4]int //2 line seriesCopy the code

So how do we access our two-dimensional array?

In the same way that we read left to right, we read the rows first and then the columns.

for rows:=0; rows<2; rows++ { for cloums:=0; cloums<4; Cloums ++{FMT.Println(arr[rows][cloums])}} arr[0][0Copy the code

This is all the content of this section, feel you watch. If you want to see more content, you can go to the home page of personal website B. Concern about 1W powder women’s pet powder…