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

What are array and slice and map in Golang?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail about array and slice and map in Golang. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

I. Array

In the go language, an array is a value type (value type)

All value type variables will produce a copy action when assigned and passed as parameters

If it is a parameter type of a function, the parameters are copied when the function is called, and the contents of the incoming array cannot be modified in the function body.

Array equality is compared with =! =, but not

< >

1. Declaration & assignment

Initialization

Grammar

The copy code is as follows:

Var VarName [n] type / / n > = 0

E.g.

Var a [5] int / / [0 0 0]

Var c [2] [3] int / / 2D

Var b int = [5] int {1st, 2pm, 3pm, 4pm, 5} / / declare and initialize

A: = [3] int {1pm 2je 3}

B: = [10] int {1J 2je 3} / / the first three elements, the others are 0

C: = [20] int {19:1} / / the 20th element is initialized to 1, the other default is 0

D: = [...] int {4pm 5pm 6} / / automatically calculate the length

E: = [...] int {0:1, 1:2, 19:3} / / automatic inference

Two-dimensional array

The copy code is as follows:

DoubleArray: = [2] [4] int {[4] int {1rec 2rem 3je 4}, [4] int {5je 6je 7je 8}}

EasyArray: = [2] [4] int {{1pm 2je 3pm 4}, {1pm 2pm 3je 4}}

Multidimensional [.] [n] the former can be inferred, but the latter must display assignments.

The length of the array is a built-in constant of the array type

ArrLength: = len (arr)

Note that the array length is also part of the type, so different length arrays are different types (built-in constants)

That is, [3] int and [4] int are different types, and the length of the array cannot be changed.

The assignment between arrays is the assignment of values, that is, when an array is passed into a function as an argument, it is actually a copy of the array (a copy operation), not its pointer. If you want to pass in a pointer, use slice.

two。 Element access

The copy code is as follows:

For iRank 0; I

< len(array); i++ { fmt.Println(i, array[i]) } for i, v := range array { fmt.Println(i, v) } 可以用new创建数组 复制代码 代码如下: p := new([10]int) 返回一个指向数组的指针 注意区分 指向数组的指针 复制代码 代码如下: a := [100]int{} var p *[100]int = &a 指针数组 复制代码 代码如下: x, y = 1, 2 a := [...]*int{&x, &y} 二.Slice 数组切片就像一个指向数组的指针,但更复杂,实际上它拥有自己的数据结构,而不仅仅是指针(指向原生数组的指针 + 数组切片中元素个数 + 数组切片已分配的存储空间) 一个引用类型,总是指向一个底层array,声明可以向array一样,只是不需要长度 slice就像一个结构体,包含三个元素 一个指针,指向数组中slice指定的开始位置 长度,即slice的长度 最大长度,也就是slice开始位置到数组的最后位置的长度 1.声明&赋值 通过array创建 复制代码 代码如下: var myArray [10]int = [10]int{1,2,3,4,5,6,7,8,9,10} var mySlice []int = myArray[:5] a := [5]int{1,2,3,4,5} b := a[2:4] b := a[:4] b := a[2:] 从数组或已存在的slice再次声明 复制代码 代码如下: var ar [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'} var a, b []byte a = ar[2:5] b = ar[3:5] 直接创建 复制代码 代码如下: myslice1 := make([]int, 5) myslice2 := make([]int, 5, 10) //初始个数5,预留10个元素的存储空间 myslice3 := []int{1,2,3,4,5} 2.元素访问 复制代码 代码如下: for i:=0; i 2 copy(slice1, slice2) //复制slice2的前三个 2 ->

one

Slice

The copy code is as follows:

The default start position 0 _ (: n) is equivalent to ar [0: n]

The second sequence defaults to the array length ar [n:] equivalent to ar [n: len (ar)]

Get slice directly from an array, which can be ar [:]

Slice is a reference type, so when you change an element in it, all other references change.

The copy code is as follows:

ASlice = array [3:7]

Bslice = aSlice [: 3]

III. Map

The concept of Dictionary in Python

Map is unordered and its length is not fixed. The built-in len can be used for map and can be easily modified.

1. Declaration & assignment

The copy code is as follows:

Map[keyType] valueType

Var m map [string] PersonInfo

M = make (map [string] personInfo [, 100])

Var numbers map[string] int

Or

Numbers: = make (map [string] int)

Numbers ["one"] = 1

Initialize a dictionary

two。 Element access

The copy code is as follows:

Rating: = map [string] float32 {"c": 5, "Go": 4.5}

CsharpRating, ok: = rating ["C #"]

If ok {

Fmt.Println ("get the value")

} else {

Fmt.Println ("error")

}

3. Basic operation

Assignment

The copy code is as follows:

M ["1234"] = PersonInfo {}

Delete

The copy code is as follows:

Delete (m, "1234")

four。 Other

Make and new operation

The copy code is as follows:

Make is used for memory allocation of built-in types (map,slice,channel).

New is used for various types of memory allocation

New is essentially the same as the function of the same name in other languages, new (T) allocates the memory space of type T filled with zero and returns its address, that is, a value of type * T, that is, it returns a pointer to the zero value of the newly allocated type T.

Make (T, args), which can only create a slice,map,channel and return a T type with an initial value (non-zero) instead of * T. In essence, the reason for the difference between the three types is that references to data structures must be initialized before they can be used

On the Golang of array and slice and map is how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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