You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
p/go-array-slice-map/
在 Go 语言中,为便于存储及管理用户数据,其数据结构设计分为数组 Array、切片 Slice、映射 Map 三种结构。
近期又看了 Go 语言基础的内容,看了一下这三种结构实现的原理:
数组 Array 数组是切片和映射的基础数据结构; 数组是长度固定的数据类型并且在内存中也是连续分配的,固索引数组数据速度是非常快的; 声明数组时需要指定数组存储的类型及数量(数组的长度); 数组变量的类型包括数组长度和元素的类型,只有两部分都相同的数组才可相互赋值。 创建及初始化 一旦声明了数组,其本身的数据类型及长度都是不可以进行变更。
1 2 3 4 5 6 7 8 9 // 使用数组字面量声明数组 array := [5]int{1, 2, 3, 4, 5} // 自动推导长度声明数组 array := [...]int{1, 2, 3, 4, 5, 6} // 使用 ... 代替长度,根据初始化元素个数推导 // 声明数组并指定特定元素值 array := [5]int{1:10, 2:20} 指针类型 数组元素的类型可以为任何内置类型,也可以是某种结构类型,也可以是指针类型。
1 2 3 4 5 6 7 // 声明一个元素长度为 3 的指向字符串的指针数组 var array1 [3]*string // 为指针数组指定元素 *array1[0] = "demo0" *array1[1] = "demo1" *array1[2] = "demo2" 多维数组 数组本身是一维数据,多维数组是由多个数组组合而来的。
https://blog.debuginn.com/p/go-array-slice-map/
Beta Was this translation helpful? Give feedback.
All reactions