GO language array, slice, MAP summary

Array

It is a set of numbered array and fixed-length data items having the same unique sequence type, this may be any type of primitive types such as plastic, string or a custom type.
The length of the array must be constant, and the length of a portion of the array type. Once defined, the length can not be changed. Arrays can be accessed by subscript, subscript is from 0, the last element index is: len-1, access violation (subscript outside the legal range), the access violation is triggered, it will panic.
An array is a value type, assignment and transfer of participants to replicate the entire array. Therefore, changing the value of copies, it does not change the value itself.

Array Declarations

var 数组变量名 [元素数量]元素类型
var a [3]int  //声明一个长度为3的整形数组
var city = [...]string{"北京", "上海", "深圳"} //声明一个可变长度的字符串类型数组并且初始化

Array defined length must be specified.
Let compiler infers the length of the array according to the number of its own initial value ....

slice

Slice (the Slice) is a sequence of variable length have the same type of element. It is based on array type package made of a layer.
Slice is a reference type, its internal structure contains the address, length, and capacity.

Slice statement

var 变量名称[]元素类型
var a []string              //声明一个字符串切片
var b = []int{}             //声明一个整型切片并初始化
var c = []bool{false, true} //声明一个布尔切片并初始化

Using the make () function constructs a slice.

make([]元素类型, 切片中元素的数量, 切片的容量)
s := make([]int, 2, 10)

Slice length need not be described.
Between slices is not comparable, and nil can only compare!

map

map is a disorder based on the key-value data structure, Go language map is a reference type, it must be initialized before use. map is disordered.

map declaration

map[键类型]值类型
make(map[KeyType]ValueType, [cap])
其中cap表示map的容量,该参数不是必须的

Guess you like

Origin www.cnblogs.com/aresxin/p/2467234365.html